简体   繁体   中英

Saving user details using SharedPreferences

Hi I want that my application to remember the login details of the user once the user has logged in until the user would press the "log out" button. I'm am using this style

private CheckBox mCheckSavePassword;
    mCheckSavePassword.setChecked(preferences.getBoolean("prefSavePassword", false));
    SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString("prefUserName", username);
            if(mCheckSavePassword.isChecked()) {
                    preferencesEditor.putString("prefPassword", password);
             }
             preferencesEditor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked());
             preferencesEditor.commit();

but it doesnt works

This is how you save a user details when a user login is successful.

Also, if you want to pupulate the username from the shared preferences for the next time when the user goes to login screen, this code will help you.

I have also included the code to populate the shared prefernces values in the EditText if the Orientation is changed.

    //Form Variables
EditText userName; 
CheckBox chkRememberMe;

//SharedPrefernces Variables to save userName and password of User
SharedPreferences loginPreferences;
private static final String SPF_NAME = "vidslogin";
private static final String USERNAME = "username";


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Getting Values from edit text fields
    userName = (EditText) findViewById(R.id.login_et_username);

    //Check box  to remember UN and PSW
    chkRememberMe = (CheckBox) findViewById(R.id.cb_rememberMe);

    //Read previously saved   userName from sharedPreferences  and populate in the editText
    loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
    userName.setText(loginPreferences.getString(USERNAME, ""));
}

//Saving Activity State
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //Read previously saved   userName & password from sharedPreferences 
        loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        userName.setText(loginPreferences.getString(USERNAME, ""));

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        //Read previously saved   userName & password from sharedPreferences 
        loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        userName.setText(loginPreferences.getString(USERNAME, ""));

    }
}

// login function

public void login(){

//Check if he is authenticated
// if Authenticated and if the user checks "REMEMBER ME"-- Checkbox, save his details
//Saving userName into SharedPreferences to read for next time 

   String strUserName = userName.getText().toString().trim();
    if (chkRememberMe.isChecked()){
    loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
    loginPreferences.edit().putString(USERNAME, strUserName).commit();
    } 
}

Hope it hepls.

see the below usage for writing

    SharedPreferences sharedpreferences = getSharedPreferences("pref_file_name", Context.MODE_PRIVATE);
    Editor editor = sharedpreferences.edit();
    editor.putString("prefUserName", username);
    if (mCheckSavePassword.isChecked()) 
        editor.putString("prefPassword", password);
    editor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked());
    editor.commit();

and you have to separate out you read logic (above is just writing it to preference) During initialization of screen just read the preference and initialize the state of checkbox and password box

Try this way,hope this will help you to solve your problem.

How to set :

SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("prefUserName", username);
editor.putBoolean("prefPassword", mCheckSavePassword.isChecked()?password:"");
editor.commit();

How to get :

SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
mCheckSavePassword.setChecked(sharedPreferences.getString("prefPassword","").trim().length()>0?true:false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM