In Android development, it is often the case that that is, you need to store user preferences (such as the user's favorite app color theme), settings related to a particular logged-on user (such as the preferences of different landing users), and settings throughout the life cycle of the app (such as the first login show introduction page) stored in the user's local, Then consider using Sharedpreferences first. This is a class that is specifically designed to store lightweight key-value pairs on Android and is stored locally as an XML file.
First, the operating mechanism
interface: Sharedpreferencesimpl in this interface, defines a editor interface, an edit method, multiple Get methods, and a listener for monitoring content changes. Where the editor interface is used primarily to write data, the Edit method is used to provide a editor instance, a Get method is used to obtain a key-value pair, and the listener is used to monitor the implementation class.
Interface: The editor interface contains multiple put methods, a commit method, and so on. The reason to write independently is to ensure the integrity of data writes. The main consideration is that if you write multiple key-value pairs at the same time, these key values will not be written to the file, but first packaged together, and then write once, that is, one-at-a-time. The implementation method is to implement a hashmap in editor to hold the data to be written, to write all the key value pairs to the HashMap in memory, and then write to the file one at a time.
This process can be summarized as first committomemory, and then WriteToFile.
Ii. Methods of Use
first, to write a key-value pair, you first get a Sharedpreferences object.
At this point, you need to provide two elements: context object, corresponding file name.
Here, we want to be clear about how these key-value pairs are actually stored: they are stored as multiple files:/data/data/{packagename}/shared_prefs/{name}.xml. where the actual {Name}.xml is specified in the program.
So, we just call Context.getsharedpreferences (name, mode) to get the Sharedpreferences object mapped to the corresponding Name.xml file in the PackageName folder. Where mode is the way to read a file.
Example:
1, the use of sharedpreferences to save data methods are as follows:
Instantiate the Sharedpreferences object (the first step)
sharedpreferences mysharedpreferences= getsharedpreferences ("test"),
Activity.mode_private);
Instantiate the Sharedpreferences.editor object (step two)
sharedpreferences.editor Editor = Mysharedpreferences.edit ();
Save the data
editor.putstring ("name", "Karl") by putstring method;
Editor.putstring ("Habit", "sleep");
Submit the current Data
editor.commit ();
Use the Toast information prompt box to prompt for a successful write to the data
Toast.maketext (this, "Data is successfully written to sharedpreferences! ", Toast.length_long). Show ();
By executing the above code, Sharedpreferences will save the data in the Test.xml file, and you can export the file under the data/data/corresponding package name/test.xml in File Explorer and view it.
2, the use of Sharedpreferences read data methods are as follows:
Similarly, to instantiate a Sharedpreferences object before reading sharedpreferences data
sharedpreferencessharedpreferences= Getsharedpreferences ("Test",
activity.mode_private);
Get value using the GetString method, note that the 2nd parameter is the default value of value
String name =sharedpreferences.getstring ("name", "");
String habit =sharedpreferences.getstring ("Habit", "");
Use the Toast information balloon to display the information
toast.maketext (this, read the data as follows: "+" \ n "+" name: "+ name +" \ n "+" habit: "+ habit,
toast.length_ LONG). Show ();
Third, understanding
we can simply understand that the local location of the mobile phone will store our custom XML file, each file is used to hold the relevant key value pairs of data. For example, we want to store the user's preferences, we can create a sharedpreferences named Userpref and read and write.
Iv. comparison of getsharedpreferenced and Getpreference
getsharedpreferenced gets files that can be acquired within the package using the context object, and you can get a specific XML file by specifying name only.
However, the Getpreference method is specifically used within the activity, and the created XML file can only be obtained within the activity context and does not need to specify a name, and this XML file belongs to this activity only.
The rest is no different. The only difference is the difference between a permission field.
Example:
Context context = getactivity ();
Sharedpreferences sharedpref = context.getsharedpreferences ("Userpref", mode_private);
Sharedpreferences sharedpref = getactivity (). Getpreferences (Context.mode_private);