Android:即使在我关闭应用程序后,用户名仍保持登录状态

Android: Keep username logged in even after I close the application

我是 Android 的新手,我正在开发一个有 3 个屏幕的应用程序:

  1. 带有登录和注册按钮的第一个屏幕;
  2. 一个注册屏幕;
  3. 我的主屏幕显示了应用程序的功能。

注册和登录工作正常,问题是我需要找到一种方法来保持应用程序登录,即使我关闭了应用程序。

这是主屏幕activity(登录屏幕在此屏幕中显示为弹出窗口):

    public class HomeActivity extends Activity
    {
        Button btnSignIn,btnSignUp;
        LoginDataBaseAdapter loginDataBaseAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // create a instance of SQLite Database
            loginDataBaseAdapter=new LoginDataBaseAdapter(this);
            loginDataBaseAdapter=loginDataBaseAdapter.open();

            // Get The Refference Of Buttons
            btnSignIn=(Button)findViewById(R.id.buttonSignIN);
            btnSignUp=(Button)findViewById(R.id.buttonSignUP);

            // Set OnClick Listener on SignUp button
            btnSignUp.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    /// Create Intent for SignUpActivity  abd Start The Activity
                    Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
                    startActivity(intentSignUP);
                }
            });
        }
        // Methos to handleClick Event of Sign In Button
        public void signIn(View V)
        {
            final Dialog dialog = new Dialog(HomeActivity.this);
            dialog.setContentView(R.layout.login);
            dialog.setTitle("Login");

            // get the Refferences of views
            final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
            final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

            Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

            // Set On ClickListener
            btnSignIn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // get The User name and Password
                    String userName=editTextUserName.getText().toString();
                    String password=editTextPassword.getText().toString();

                    // fetch the Password form database for respective user name
                    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                    // check if the Stored password matches with  Password entered by user
                    if(password.equals(storedPassword))
                    {
                        Toast.makeText(HomeActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                        //SharedPreferences 

                        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putInt(getString(userName), new_login);
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putInt(getString(password), new_password);

                        editor.commit();

                        //Teste pra mandar o usuario

                        Intent i = new Intent(getApplicationContext(), wheel.class);
                        i.putExtra("new_variable_name",userName);

                        //Teste pra mandar o usuario

                        Intent browserIntent =
                                new Intent(HomeActivity.this, wheel.class);

                        startActivity(browserIntent);
                        startActivity(i);

                    }
                    else
                    {
                        Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                    }
                }
            });

            dialog.show();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            // Close The Database
            loginDataBaseAdapter.close();
        }
     }

这是我的注册activity:

public class SignUPActivity extends Activity
{
    EditText editTextUserName,editTextPassword,editTextConfirmPassword;
    Button btnCreateAccount, btn_home;

    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        // get Instance  of Database Adapter
        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();

        // Get Refferences of Views
        editTextUserName=(EditText)findViewById(R.id.editTextUserName);
        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);

        btn_home=(Button)findViewById(R.id.btn_home);
        btn_home.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent browserIntent =
                        new Intent(SignUPActivity.this, HomeActivity.class);
                startActivity(browserIntent);
            }
        });

        btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
        btnCreateAccount.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                String userName=editTextUserName.getText().toString();
                String password=editTextPassword.getText().toString();
                String confirmPassword=editTextConfirmPassword.getText().toString();

                // check if any of the fields are vaccant
                if(userName.equals("")||password.equals("")||confirmPassword.equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                    return;
                }
                // check if both password matches
                if(!password.equals(confirmPassword))
                {
                    Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                    return;
                }
                else
                {
                    // Save the Data in Database
                    loginDataBaseAdapter.insertEntry(userName, password);
                    Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }
}

将认证数据保存在SharedPreferences中。并在应用程序打开时检查它,无论用户是否登录。

编辑 1:

以下是您将如何 write data to SharedPreferences

然后read data from SharedPreferences

希望对您有所帮助!

我认为这可能对您有所帮助:http://codeasp.net/blogs/android/1662/shared-preferences-in-android-applications

即使您在按下 "LOAD DATA" 按钮时关闭应用程序,您也可以获取保存的登录详细信息。