Android data storage - shared perences

Posted by suneel on Sat, 04 Apr 2020 04:32:14 +0200

SharedPerences

  • Storage mode: key value pair

Store data in SharedPreferences

Step:

  1. Call the edit() method of the SharedPerences object to get a SharedPerences.Editor object.
  2. Add data to sharedperence.editor.
  3. Call the commit() method to commit the added data.

    In the click event:

SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
//Specify the file name as data
                editor.putString("name","sll");
                editor.putBoolean("married",false);
                editor.putInt("age",18);
                editor.commit();

Call the edit() method of the SharedPreferences object to get a SharedPreferences. Editor object.

Read data from SharedPerferences

In the click event:

 SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
                String name = pref.getString("name","");
                int age = pref.getInt("age",0);
                boolean married =pref.getBoolean("married",false);

Realize the function of remembering password

Continue to modify the code on the previous program:

  • Modify the login.xml file and add a check box -- check box control
<TableRow>

        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_height="wrap_content"
            android:text="Remember password" />
    </TableRow>
  • Modify code in LoginActivity
public class LoginActivity extends BaseActivity {
//
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberPass;
////
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        accountEdit = (EditText)findViewById(R.id.account);
        passwordEdit = (EditText)findViewById(R.id.password);
//
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        rememberPass = (CheckBox)findViewById(R.id.remember_pass);
        boolean isRemember = pref.getBoolean("remember_passeord",false);
        //After getting the object, call its getBoolean() method to get the value corresponding to the key "remember" password. The default value is of course "cause".
        if(isRemember){
            //Set account and password in text box
            //The second time the code logs in, it runs: isRemember is true at that time.
            String account = pref.getString("account","");
            String password = pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);

        }
////
        login = (Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if(account.equals("admin")&&password.equals("123456")){

//                    editor=pref.edit();
                    if(rememberPass.isChecked()){
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",account);
                        editor.putString("password",password);
                        //Selected: set remember? Password to true, and then save the corresponding value to a file and submit it.
                    }else{
                        editor.clear();
                        //Unchecked: clear data from file
                    }
                    editor.commit();
////
                    Intent intent = new  Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

Topics: Android xml