A. What is Sharedpreferences?
Sharedpreferences is one of the four data storage technologies for Android (Sharedpreferences, SQLite, Content provider, and file), Sharedpreferences is dealing with a key-value (key-value pair), often used to store some lightweight data.
(official website is the best place to learn Android)
Website Link: http://developer.android.com/reference/android/content/SharedPreferences.html
Second, simple use
1. Create a sharecontents class to hold the Key-value key
public class Sharecontents {public static string Sharename= "ShareName";p ublic static string myName = "MyName";p ublic Stati C String isgood = "Isgood";}
2. Create a Sharemanager class that is used to manage Sharedpreferences's get and set methods uniformly, equivalent to the value set or Key-value.
Import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;public class Sharemanager {private Sharedpreferences share;private Editor Editor;private String TAG = "Sharemanager";p rivate Sharemanager () {super ();} public void Clear () {editor.clear (). commit ();}; Public Sharemanager (Context context) {super (); share = Context.getsharedpreferences (Sharecontents.sharename, context.mode_private); editor = Share.edit ();} /** * Get myName * * @return */public string Getmyname () {string result = Share.getstring (Sharecontents.myname, ""); return Result;} /** * Set MyName * * @param myName */public void Setmyname (String myName) {editor.putstring (Sharecontents.myname, myName). C Ommit ();} /** * Get Isgood * @return */public boolean Isgood () {Boolean result = Share.getboolean (Sharecontents.isgood, false);; return result;} /** * Set Isgood * @param isgood */public void Setisgood (Boolean isgood) {Editor.putboolean (Sharecontents.isgood, IsGood). Commit ();}}
3. Demo Demo of Sharedpreferences
public class Sharepreferencesactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate), final button button = New button (this), Setcontentview (button), Button.settext (" Click on the screen, you can set the sharedpreferences and get the value displayed on the screen ");//Generate object sharemanagerfinal sharemanager Sharemanager = new Sharemanager ( this);//Set name Sharemanager.setmyname ("Li Xiao Long");//Set Isgood to Truesharemanager.setisgood (true); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (view view) {Button.settext ("MyName:" + Sharemanager.getmyname () + " ; "+" Isgood: "+sharemanager.isgood ());};}}
You can click here to find the demo of this section
This article link: http://www.cnblogs.com/liqw/p/4148411.html
Sharedpreferences used in Android project specification