In our usual use of mobile apps, you only need to log on to the account once, and then go to the application for the second time to directly jump to the effect interface, in addition, how does the QQ login box remember our stealth login and save the account options? These are all achieved by using SharedPreferences to share the parameter effect, instead of using a database for storage. The following describes the detailed code analysis.
Package com. example. account. login; import java. util. hashMap; import java. util. map; import com. android. dao. mySQLiteOpenHelper; import com. example. account. mainActivity; import com. example. account. r; import android. app. activity; import android. content. context; import android. content. intent; import android. content. sharedPreferences; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class LoginActivity extends Activity {private EditText e1, e2; private SQLiteOpenHelper helper; private boolean flag, flag2, flag3; private HashMap
Map; @ SuppressWarnings ("unchecked") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. login); TextView textView = (TextView) this. findViewById (R. id. textView1); e1 = (EditText) this. findViewById (R. id. editText1); e2 = (EditText) this. findViewById (R. id. editText2); // obtain data map = (HashMap
) GetMsg ("login"); if (map! = Null &&! Map. isEmpty () {if (Boolean) map. get ("login2") {// if the value is true, you do not need to enter a password and directly jump to the operation interface to enter Intent intent = new Intent (LoginActivity. this, MainActivity. class); startActivity (intent) ;}} helper = new MySQLiteOpenHelper (this); textView. setText ("Logon interface"); Button button Button = (Button) findViewById (R. id. button2); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (! E1.getText (). toString (). isEmpty ()&&! E2.getText (). toString (). isEmpty () {// obtain account information from the database SQLiteDatabase database = helper. getReadableDatabase (); Cursor cursor = database. query ("user", new String [] {"username", "password"}, null, null); while (cursor. moveToNext () {flag = e1.getText (). toString (). equals (cursor. getString (cursor. getColumnIndex ("username"); flag2 = e2.getText (). toString (). equals (cursor. getString (cursor. getColumnIndex ("password"); if (flag & flag2) {Intent intent = new Intent (LoginActivity. this, MainActivity. class); startActivity (intent); // overridePendingTransition (R. anim. zoomin, R. anim. zoomout); Toast. makeText (LoginActivity. this, "Logon successful", Toast. LENGTH_SHORT ). show (); flag3 = true; // after successful login, set the flag to the HashMap stored in the sharing Parameter
Map = new HashMap
(); Map. put ("login2", flag3); saveMsg ("login", map) ;}} if (! Flag3) {Toast. makeText (LoginActivity. this, "the account or password you entered is incorrect", Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (LoginActivity. this, "enter your account password correctly", Toast. LENGTH_SHORT ). show () ;}}); Button button2 = (Button) findViewById (R. id. button1); button2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (LoginActivity. this, RegisterActivity. class); startActivity (intent) ;}}) ;}// enter the shared parameter public boolean saveMsg (String fileName, Map
Map) {boolean flag = false; // The general Mode uses private. It is safer to use SharedPreferences preferences = getSharedPreferences (fileName, Context. MODE_PRIVATE); SharedPreferences. editor editor = preferences. edit (); // The Map class provides a method called entrySet (), which returns a Map. object set after Entry instantiation. // Next, Map. the Entry class provides a getKey () method and a getValue () method. // Therefore, the above Code can be organized to better conform to the logic for (Map. entry
Entry: map. entrySet () {String key = entry. getKey (); Object object = entry. getValue (); // Add if (object instanceof Boolean) {Boolean new_name = (Boolean) object; editor. putBoolean (key, new_name);} else if (object instanceof Integer) {Integer integer = (Integer) object; editor. putInt (key, integer);} else if (object instanceof Float) {Float f = (Float) object; editor. putFloat (key, f);} else if (object instanceof Long) {Long l = (Long) object; editor. putLong (key, l);} else if (object instanceof String) {String s = (String) object; editor. putString (key, s) ;}} flag = editor. commit (); return flag;} // read data public Map
GetMsg (String fileName) {Map
Map = null; // The editSharedPreferences preferences = getSharedPreferences (fileName, Context. MODE_APPEND); // Context. MODE_APPEND can modify the existing value map = preferences. getAll (); return map ;}}