Remember password in android (sharedpreferences)

Source: Internet
Author: User

The password-remembering function of the login interface in Android is implemented to store the user's account number and password in sharedpreferences mode (note that the password is encrypted with MD5 plaintext).


Interface XML file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <edittext android:id=" @+id/et_qq "android:layout_width=" Fill_parent "Android : layout_height= "wrap_content" android:hint= "Please enter your QQ number"/> <edittext android:inputtype= "Textpassword "Android:id=" @+id/et_password "android:layout_width=" fill_parent "android:layout_height=" Wrap_conten T "android:layout_below=" @id/et_qq "android:hint=" Please enter the password "/> <checkbox android:id=" @+id/cb_re Meber_pwd "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_bel         ow= "@id/et_password" android:text= "Remember Password"/> <linearlayout android:layout_below= "@id/cb_remeber_pwd" Android:layout_width= "Fill_parent"        android:layout_height= "wrap_content" android:orientation= "Horizontal" > <button and            Roid:id= "@+id/bt_ok" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "OK"/> </LinearLayout></RelativeLayout>


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;/*** * * * *. Create a Sharedpreferences * *. Initialize the sharedpreferences parameter 1 SP's file name parameter 2 SP's save mode. To save the data to the SP first, get a text editor editor. Save the data remember to perform commint () saving the data. Read Data sp.ge Tstring () Sp.getint (); */public class Mainactivity extends Activity {private EditText et_qq;private EditText et_password;private CheckBox Cb_rem Eber_pwd;private Button bt_ok;/** * A convenient API */private Sharedpreferences SP for data storage under Android system; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /completes the initialization of 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);//Gets the data stored in the SP SAVEDQQ = sp.getstring ("QQ", "" "); String Savedpassword = sp.getstring ("Password", "" "); Et_qq.settext (SAVEDQQ); Et_password.settext (Savedpassword); bt_ OK = (button) Findviewbyid (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 password cannot be empty", 0). Show ();} else {// Check to see if the user has selected the option to remember the password. The IF (cb_remeber_pwd.ischecked ()) {//Description check box is selected. The user name and password to record//get to a parameter file editor. Editor editor = Sp.edit (); Editor.putstring ("QQ", QQ), editor.putstring ("Password", Md5utils.encode (password));// Save the data to SP inside Editor.commit (); Toast.maketext (Getapplicationcontext (), "User information has been saved", 1). Show ();}}});}}


Mainactivity.java called the Md5utils.java file:

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 for the account and password is as follows:


To export the CONFIG. xml file, you can see the data from the user's password before and after the MD5 plaintext encryption:


Remember password in android (sharedpreferences)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.