Use of Sharedpreferences
In some cases, the application has a small amount of data to save, and the format of the data is simple: it is a common string, scalar type of value, such as the application of various configuration information (such as whether to open the sound effects, whether to use vibration effect, etc.), small game player points (such as minesweeper), etc., for this data , Android provides sharedpreferences for saving.
Introduction to Sharedpreferences and editor
The data that Sharedpreferences saves is primarily data that resembles the configuration information format, so the data it holds is primarily a simple type of key-value pair.
The Sharedpreferences interface is primarily responsible for reading the application's preferences data, which provides the following common methods for accessing Key-value pairs in sharedpreferences.
- Boolean contains (String key): Determines whether sharedpreferences contains data for a specific key.
- Abstract map<string,?> getAll (): Gets all the Key-value pairs in the sharedpreferences data.
- Boolean getXxx (String key,xxx defaultvalue): Gets the value corresponding to the specified key in the sharedpreferences data. If the key does not exist, the default value of DefaultValue is returned. where xxx can be a Boolean, float, int, long, string, and a variety of basic types of values.
The Sharedpreferences interface itself does not provide the ability to write data, but instead, through the internal interface of Sharedpreferences, Sharedpreferences calls the edit () method to get the editor object it corresponds to. Editor provides the following methods to write data to sharedpreferences.
- Sharedpreferences.editor Clear (): Empties all data in the sharedpreferences.
- Sharedpreferences.editor putxxx (String key,xxx value): The data corresponding to the specified key is deposited to the sharedpreferences. where xxx can be a Boolean, float, int, long, string, and a variety of basic types of values.
- Sharedpreferences.editor Remove (String key): Deletes the data item corresponding to the specified key in the sharepreferences.
- Boolean commit (): When editor is finished editing, the method is called to commit the modification.
The sharedpreferences itself is an interface, and the program cannot create the Sharedpreference instance directly, only through the getsharedpreferences provided by the context (String Name,int mode) method to get the Sharedprefereces instance, the second parameter (mode) of the method supports the following values.
- Context.mode_private: Specifies that the sharedpreferences data can only be read and written by the application.
- Context.mode_world_readable: Specifies that the sharedpreferences data can be read by other applications, but cannot be written.
- Context.mode_world_writeable: Specifies that the sharedpreferences data can be read and written by other applications.
Instance
Instance writes to the Sharedpreferences, reads the data, the program interface is simple, it just provides two buttons, one writes the data, the other is used to read the data. Layout file is relatively simple, it is not posted out.
Public classSharepreferencesactivityextendsActivity {sharedpreferences preferences; Sharedpreferences.editor Editor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_share_preferences); //get Sharedpreferences objects that can only be read and written by this applicationPreferences=getsharedpreferences ("Crazyit", mode_private); Editor=Preferences.edit (); Button Read=(Button) Findviewbyid (R.id.read); Button Write=(Button) Findviewbyid (r.id.write); Read.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Reading string DataString time=preferences.getstring ("Time",NULL); //reading data of type int intRandnum=preferences.getint ("random", 0); String result=time==NULL?" You have not yet written the data ":" Write Time: "+time+" \ nthe last generated random number is: "+Randnum; //Use Toast prompt informationToast.maketext (Getapplicationcontext (), result, Toast.length_short). Show (); } }); Write.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSimpleDateFormat adf=NewSimpleDateFormat ("yyyy mm month DD month" + "HH:MM:SS"); //Deposit Current TimeEditor.putstring ("Time", Adf.format (NewDate ())); Editor.putint ("Random", (int) (Math.random () *100)); //submit all the data depositedEditor.commit (); } }); }}
There is a crazyit.xml file in/data/data/com.hutao.sharedpreference/shared_prefs/. Open the XML document to see the contents of the file:
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><map><int name= "random" value= "30"/ ><string name= "Time" >2014 year November March 03:29:23</string></map>
Resources:
"Android Madness Handout"
Android Data Storage--sharepreferences