Transferred from: http://blog.csdn.net/jingfeizhu/article/details/10017779
Sharedpreferences 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. The steps to implement Sharedpreferences storage are as follows:
First, according to the context to obtain Sharedpreferences object
Second, use the edit () method to get the editor object.
Third, through the editor object store Key-value key value pair data.
Iv. submit data through the commit () method.
1 Public classMainactivityextendsActivity {2 @Override3 Public voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.main);6 7 //Get Sharedpreferences Object8Context CTX = mainactivity. This; 9Sharedpreferences sp = ctx.getsharedpreferences ("pref", mode_private);Ten //Deposit Data OneEditor Editor =Sp.edit (); AEditor.putstring ("name", "Jeff"); -Editor.putint ("Score", 0); -Editor.putboolean ("Playagain",true); the editor.commit (); - - } -}
The above example is cumbersome to use, and here is a simple encapsulation scheme:
1 ImportJava.util.Map.Entry;2 3 4 Importandroid.content.ContentValues;5 ImportAndroid.content.Context;6 Importandroid.content.SharedPreferences;7 8 9 Public classMypreference {Ten One Private Static FinalString preference_name = "My.db"; A - Privatesharedpreferences mpreference; - the Publicmypreference (Context ctx) { -Mpreference =Ctx.getapplicationcontext (). Getsharedpreferences (Preference_name, context.mode_private); - } - + - Public Booleanputstringandcommit (string key, String value) { + returnMpreference.edit (). putstring (key, value). commit (); A } at - - Public BooleanPutintandcommit (String Key,intvalue) { - returnMpreference.edit (). Putint (key, value). commit (); - } - in Public BooleanPutbooleanandcommit (String Key,Booleanvalue) { - returnMpreference.edit (). Putboolean (key, value). commit (); to } + - Public Booleanputintandcommit (contentvalues values) { theSharedpreferences.editor Editor =Mpreference.edit (); * for(Entry<string, object>Value:values.valueSet ()) { $ editor.putstring (Value.getkey (), Value.getvalue (). toString ());Panax Notoginseng } - returneditor.commit (); the } + A the Publicstring getString (String key) { + returnGetString (Key, ""); - } $ $ Publicstring getString (String key, String defvalue) { - returnmpreference.getstring (key, defvalue); - } the - Public intgetInt (String key) {Wuyi returnGetInt (Key, 1); the } - Wu - Public intGetInt (String Key,intdefvalue) { About returnmpreference.getint (key, defvalue); $ } - - Public BooleanGetboolean (String key) { - returnGetboolean (Key,false); A } + the Public BooleanGetboolean (String Key,Booleandefvalue) { - returnMpreference.getboolean (key, defvalue); $ } the}
Use the following methods:
1 Public classMainactivityextendsActivity {2 @Override3 Public voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.main);6 7Mypreference pref =NewMypreference ( This);8Pref.putstringandcommit ("name", "Jeff");9 Ten } One}
Sharedpreferences Practical tips for Android