The recent Android project requires an automatic login feature, which, after completion, summarizes this feature, which relies on sharedpreferences for implementation.
sharedpreferences IntroductionSharedpreferences is also a lightweight method of data storage, which is essentially based on XML files that store Key-value key-value pairs of data, typically used to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object.
sharedpreferences Use instance: Remember user name password automatic loginAfter a rough overview of sharedpreference, let's look at an example that remembers the user name password auto-login:
package com.dt5000.ischool.activity; import Android.annotation.SuppressLint; import Android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import Android.content.SharedPreferences.Editor; import Android.os.Bundle; import Android.util.Log; import android.view.KeyEvent; import Android.view.View; import Android.widget.CheckBox; import Android.widget.EditText; import Com.dt5000.ischool.util.DTUtil; import com.dt5000.ischool.util.MyApplication; /** * @author: duanyr * @ Created: 2012-11-13 pm 2:36:47 * * @ Class Description: Login interface */ @SuppressLint ("Worldreadablefiles") public class Loginactivity extends DTUtil { private static final String TAG = "User Login"; Private EditText username; private EditText password; Private CheckBox AutoLogIn Private sharedpreferences sharedpreferences; private String message; @Override public void onCreate (Bundle savedinstancestate) { &N Bsp Super.oncreate (savedinstancestate); myapplication.getinstance (). addactivity (this); sharedpreferences = this.getsharedpreferences ("UserInfo", Context.mode_world_ readable); if (Sharedpreferences.getboolean ("Auto_ischeck", False)) { & nbsp Intent Intent = new Intent (); Intent.setclass (Loginactivity.this, Mainactivity.class); startactivity (intent); } else { Setcontentview (r.layout.activit Y_login); &NBsp Initview (); } } /** * Initialize view control */ public void Initview () { LOG.I (TAG, "Initialize View Control") ); username = (EditText) Findviewbyid (r.id.username); password = (EditText) Findviewbyid (R.id.password); Autologin = (CheckBox) Findviewbyid (R.id.autologin); //default remember user name Username.settext ( Sharedpreferences.getstring ("UserName", "")); /** * The method that is triggered when the login button is clicked * @param VI ew */ public void userlogin (view view) { ST Ring usernamestring = Username.gettext (). toString (); String passwordstring = Password.gettext (). toString (); if (ValidateUser (usernamestring, passwordstring)) { & nbsp Editor editor = Sharedpreferences.edit (); editor.putstring ("UserName", usernamestring); if (autologin.ischec Ked ()) {//auto-login editor.putstring ("password", passwordstring) ; Editor.putboolean ("Auto_ischeck", true). commit (); } editor.commit (); Intent Intent = new Intent (); Intent.setclass (Loginactivity.this, mainactivity.cl); startactivity (intent); } else { alert (this, message); &NBSP;&NB Sp } } //Visitor login public void Visitorlogin (View View) { Intent Intent = new Intent (), Intent.setclass ( Loginactivity.this, Mainactivity.class); startactivity (intent); /** * Verify user name password is correct * &NBSP;&NB Sp * @param username * @param password * @return */ Public boolean ValidateUser (string username, string password) { &N Bsp Boolean flag = false; try { /... Here is the service that invokes the Web service, validates the username password, and hereby omits flag = true; } catch (Exception e) { //TODO Auto-gen Erated Catch block LOG.E (TAG, E.getmessage ()); message = "Connection Server failed"; } return flag; /** * The method that is triggered when you click the Exit button */ & nbsp public void Logout_listener (view view) { dialog_exit (this); } /** * Listen back button, this is the sign-in interface to return the words given exit prompt */ & nbsp public boolean onKeyDown (int keycode, keyevent event) { if (keycode = = Keyevent.keycode_ Back &Amp;& Event.getrepeatcount () = = 0) { dialog_exit (this); &N Bsp return false; } return false; } }
page:
Sharedpreferences Implement automatic login remembering username password