Remember the password in Android (SharedPreferences)
Remember the password feature on the logon interface of Android, and store the account and password entered by the user in SharedPreferences mode (note that the password should be encrypted in MD5 plaintext ).
Interface xml file:
MainActivity. java file:
Import android. OS. bundle; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. text. textUtils; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. checkBox; import android. widget. editText; import android. widget. toast;/***** 1. create a Share DPreferences * 2. initialize the storage mode of file name parameter 2 sp of SharedPreferences parameter 1 sp * 3. to save data to sp, first obtain a text Editor * 4. remember to execute commint () to save data after data is stored * 5. read data sp. getString () sp. getInt (); */public class MainActivity extends Activity {private EditText et_qq; private EditText et_password; private CheckBox cb_remeber_pwd; private Button bt_ OK; /*** a convenient API for data storage in the android system */private SharedPreferences sp; @ Overrideprotected void oncret Ate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the sp. Sp = getSharedPreferences ("config", MODE_PRIVATE); et_qq = (EditText) findViewById (R. id. et_qq); et_password = (EditText) findViewById (R. id. et_password); cb_remeber_pwd = (CheckBox) findViewById (R. id. cb_remeber_pwd); // obtain the data stored in the sp String savedQQ = sp. getString ("qq", ""); String savedPassword = sp. getString ("password", ""); et_qq.setText (savedQQ); et_password.setText (savedPassword); bt_ OK = (Button) findViewB YId (R. id. bt_ OK); // register a click event for the button. Bt_ OK .setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {String qq = et_qq.getText (). toString (); String password = et_password.getText (). toString (); if (TextUtils. isEmpty (qq) | TextUtils. isEmpty (password) {Toast. makeText (getApplicationContext (), "Sorry, QQ number" + "or the password cannot be blank", 0 ). show ();} else {// check whether the user has checked the Remember password option. If (cb_remeber_pwd.isChecked () {// the check box is selected. Record the user name and password // get the editor of a parameter file. Editor editor = sp. edit (); editor. putString ("qq", qq); editor. putString ("password", MD5utils. encode (password); // Save the data to the editor in the sp. commit (); Toast. makeText (getApplicationContext (), "user information saved", 1 ). show ();}}}});}}
The MD5utils. java file called by MainActivity. java:
Package com. itheima. qqlogin; import java. security. messageDigest; import java. security. noSuchAlgorithmException; import android. OS. message; public class MD5utils {/*** md5-encrypted tool class ** @ param password * @ return */public static String encode (String password) {try {MessageDigest digest = MessageDigest. getInstance ("md5"); byte [] results = digest. digest (password. getBytes (); StringBuilder sb = new StringBuilder (); for (byte B: results) {int number = B & 0xff; String hex = Integer. toHexString (number); if (hex. length () = 1) {sb. append ("0");} sb. append (hex);} return sb. toString ();} catch (Exception e) {e. printStackTrace (); return "";}}}
The storage path of the account and password is as follows:
Export the config. xml file to view the data in the xml file before and after the MD5 plaintext encryption of the user password: