Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android检查用户是否已经登录_Android_Login_Sharedpreferences - Fatal编程技术网

android检查用户是否已经登录

android检查用户是否已经登录,android,login,sharedpreferences,Android,Login,Sharedpreferences,我想检查用户是否已登录应用程序,因此,如果用户未登录,他将被定向到Login.class,如果用户已登录,他将被定向到他的Profile.class 我看到了一个关于使用SplashActivity.class检查从互联网登录的用户的教程,但是我试过了,但没有对我起作用,我不知道也许我只是不了解它是如何工作的,因为我是编程新手 我的代码: 登录类 public class DocLogin extends Fragment { ImageView ivIcon; Tex

我想检查用户是否已登录应用程序,因此,如果用户未登录,他将被定向到
Login.class
,如果用户已登录,他将被定向到他的
Profile.class

我看到了一个关于使用
SplashActivity.class
检查从互联网登录的用户的教程,但是我试过了,但没有对我起作用,我不知道也许我只是不了解它是如何工作的,因为我是编程新手

我的代码:

登录类

    public class DocLogin extends Fragment {

    ImageView ivIcon;
    TextView tvItemName, tvRegister;
    EditText user, pass;
    Button btnLogin;
    String username, password;
    SQLController dbcon;
    SharedPreferences sh_Pref;
    Editor editor;

    private static final String IS_LOGIN = "IsLoggedIn";
    int PRIVATE_MODE = 0;


    public DocLogin() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.doc_log_in, container, false);

        dbcon = new SQLController(getActivity());
        dbcon.open();

        // Log in
        user = (EditText) view.findViewById(R.id.etUser);
        pass = (EditText) view.findViewById(R.id.etPassword);
        btnLogin = (Button) view.findViewById(R.id.btnLogin);

        // Sign up
        tvRegister = (TextView) view.findViewById(R.id.tvRegDoc);
        tvRegister.setMovementMethod(LinkMovementMethod.getInstance());
        tvRegister.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.content_frame, new DocReg());
                ft.commit();
                return false;
            }
        });

        // btnLogin onClickListener
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub

                dbcon = new SQLController(getActivity());
                dbcon.open();

                username = user.getText().toString();
                password = pass.getText().toString();
                sharedPreferences();
                Toast.makeText(getActivity(), "Login Successful", 20).show();

                Log.v("LoginDetails", user.getText().toString() + "../.."
                        + pass.getText().toString());
                Cursor cur = dbcon.getuser_information(user.getText()
                        .toString(), pass.getText().toString());
                if (cur.getCount() != 0) {
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.content_frame, new DocProfile());
                    ft.commit();
                } else {
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            getActivity()).create();
                    alertDialog.setTitle("Login Error");
                    alertDialog
                            .setMessage("Doctor Code and Password does not match");
                    alertDialog.setButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    // dismiss dialog
                                }
                            });
                    alertDialog.show();
                }

            }
        });

        return view;
    }


    public void sharedPreferences() {

        sh_Pref = getActivity().getSharedPreferences("Login Credentials", PRIVATE_MODE);
        editor = sh_Pref.edit();
        editor.putBoolean(IS_LOGIN, true);
        editor.putString("Username", username);
        editor.putString("Password", password);
        editor.commit();
    }

}

请指导我实现目标的正确方法。

您需要在数据库的logintable中添加alreadylogin字段。如果用户首次登录,请将alreadylogin字段设置为true,否则默认为false。每次都必须检查alreadylogin字段。如果该字段为true,则必须重定向。

您的代码显示您的首选项已设置。因此,请将以下内容放在您想要检查是否登录的位置

SharedPreferences sh_Pref = getActivity().getSharedPreferences("Login Credentials", PRIVATE_MODE);
boolean check = sh_Pref.getBoolean(IS_LOGIN, false);
if(check){
    Intent intent = new Intent(this, Profile.class);
    startActivity(intent);
    finish();
}

如果用户已经登录,则用户将被定向到
配置文件。class

我可以在SplashActivity中使用此代码段吗?您可以在应用程序中要检查登录的任何位置使用此代码段。