SharedPreferences and sharedpreferences
(1) Layout file, a simple login 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" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <TextView android: id =" @ + id/textView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: layout_marginLeft = "22dp" android: layout_marginTop = "22dp" android: text = "username:"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView1" android: layout_alignBottom = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10"/> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignRight = "@ + id/textView1" android: layout_below = "@ + id/editText1" android: layout_marginTop = "17dp" android: text = "Password:"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/editText1" android: layout_below = "@ + id/editText1" android: EMS = "10" android: inputType = "textPassword"> <requestFocus/> </EditText> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/checkBox1" android: layout_marginLeft = "34dp" android: layout_marginTop = "32dp" android: layout_toRightOf = "@ + id/button1" android: text = "cancel"/> <CheckBox android: id = "@ + id/checkBox1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/textView1" android: layout_below = "@ + id/editText2" android: layout_marginTop = "22dp" android: text = "Remember User Name"/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/button2" android: layout_alignBottom = "@ + id/button2" android: layout_alignLeft = "@ + id/editText2" android: text = "login"/> <CheckBox android: id = "@ + id/checkBox2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/checkBox1" android: layout_alignBottom = "@ + id/checkBox1" android: layout_marginLeft = "14dp" android: layout_toRightOf = "@ + id/button1" android: text = "mute login"/> </RelativeLayout>
(2) directory structure:
(3) SharedPreferences tool class LoginService. java
Package com. lc. data_storage_pai.sharepreference; import java. util. map; import android. content. context; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; public class LoginService {private Context context; // Context public LoginService (context Context) {this. context = context;}/** save logon information */public boolean saveLoginMsg (String name, String password) {boolean flag = fa Lse; // do not add a suffix, the system automatically uses. save in xml format // here, login is the file name to be stored. SharedPreferences preferences = context. getSharedPreferences ("login", context. MODE_PRIVATE + context. MODE_APPEND); Editor editor = preferences. edit (); editor. putString ("name", name); editor. putString ("password", password); flag = editor. commit (); return flag;}/** save file */public boolean saveSharePreference (String filename, Map <String, Object> map) {boolean f Lag = false; SharedPreferences preferences = context. getSharedPreferences (filename, Context. MODE_PRIVATE);/** use Editor */Editor editor = preferences to store data. edit (); for (Map. entry <String, Object> entry: map. entrySet () {String key = entry. getKey (); Object object = entry. getValue (); if (object instanceof Boolean) {Boolean new_name = (Boolean) object; editor. putBoolean (key, new_name);} else if (object in Stanceof 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 File */public Map <String,?> GetSharePreference (String filename) {Map <String,?> Map = null; SharedPreferences preferences = context. getSharedPreferences (filename, Context. MODE_PRIVATE);/** when reading data, you only need to access it */map = preferences. getAll (); return map ;}}
(3) MainActivity. java
Package com. lc. data_storage_share; import java. util. hashMap; import java. util. map; import com. example. data_storage_pai.r; import com. lc. data_storage_pai.sharepreference.loginservice; import android. OS. bundle; import android. app. activity; 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;/** the unit test must be performed in the list file */public class MainActivity extends Activity {private Button button1; // log on to private Button button2; // cancel private EditText editText1, editText2; private CheckBox checkBox1; // remember the password private CheckBox checkBox2; // mute login private LoginService service; Map <String,?> Map = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2); editText1 = (EditText) this. findViewById (R. id. editText1); editText2 = (EditText) this. findViewById (R. id. editText2); checkBox1 = (CheckBox) this. findViewBy Id (R. id. checkBox1); checkBox2 = (CheckBox) this. findViewById (R. id. checkBox2); service = new LoginService (this); map = service. getSharePreference ("login"); if (map! = Null &&! Map. isEmpty () {editText1.setText (map. get ("username "). toString (); checkBox1.setChecked (Boolean) map. get ("isName"); checkBox2.setChecked (Boolean) map. get ("isquiet");} button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubif (editText1.getText (). toString (). trim (). equals ("admin") {Map <String, Object> map1 = new HashMap <String, Object> (); if (checkBox1.isChecked () {map1.put ("username ", editText1.getText (). toString (). trim ();} else {map1.put ("username", "");} map1.put ("isName", checkBox1.isChecked (); map1.put ("isquiet ", checkBox2.isChecked (); service. saveSharePreference ("login", map1) ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
(4) test class:
Package com. lc. data_storage_share; import java. util. hashMap; import java. util. map; import android. test. androidTestCase; import android. util. log; import com. lc. data_storage_mirror.sharepreference.loginservice; public class MyTest extends AndroidTestCase {private final String TAG = "MyTest"; public MyTest () {// TODO Auto-generated constructor stub}/** log on */public void save () {LoginService service = new Logi NService (getContext (); boolean flag = service. saveLoginMsg ("admin", "123"); Log. I (TAG, "-->" + flag);}/** save file */public void saveFile () {LoginService service = new LoginService (getContext ()); map <String, Object> map = new HashMap <String, Object> (); map. put ("name", "jack"); map. put ("age", 23); map. put ("salary", 23000.0f); map. put ("id", 1256423132l); map. put ("isManager", true); boolean flag = service. save SharePreference ("msg", map); Log. I (TAG, "-->" + flag);}/** Read File */public void readFile () {LoginService service = new LoginService (getContext ()); map <String,?> Map = service. getSharePreference ("msg"); Log. I (TAG, "-->" + map. get ("name"); Log. I (TAG, "-->" + map. get ("age"); Log. I (TAG, "-->" + map. get ("salary"); Log. I (TAG, "-->" + map. get ("isManager"); Log. I (TAG, "-->" + map. get ("id "));}}
How to add a Junit test unit:
1. Add the following in the configuration file:
2. Add:
3. The test class MyTest must inherit AndroidTestCase
(5) As a result, the user name will be logged in next time.