In Android development, it is often encountered that user preference settings (such as user preference app color themes), settings related to specific login users (such as different login users' preferences), and settings throughout the life cycle of the app (such as first login displaying the introduction page) are stored locally. Then first consider using sharedPreferences. This is a class specially used in Android to store lightweight key-value pair data, which will be stored locally as an XML file.
1. Operating mechanism
Interface: SharedPreferencesImpl In this interface, it defines an Editor interface, an edit method, multiple get methods, and a listener for monitoring content changes. Among them, the Editor interface is mainly used to write data, the edit method is used to provide an editor instance, the get method is used to obtain key-value pairs, and the listener is used to implement listening in the class.
Interface: The Editor interface contains multiple put methods, a commit method, etc. The reason why an interface is needed to write is to ensure the integrity of data writing. The main scenario is that if multiple key-value pairs are to be written at the same time, these key-value pairs will not be written to the file one by one, but will be packaged together first and then written at one time, that is, one-at-a-time. The specific implementation method is to implement a hashMap in Editor to temporarily store the data to be written, first write all key-value pairs into the hashMap in memory, and then write them to the file at once.
This process can be summarized as commitToMemory first, and then writeToFile.
2. How to use
First, if you want to write a key-value pair, you must first get a sharedPreferences object.
At this time, two elements need to be provided: the context context object, and the corresponding file name.
Here, we need to clarify the actual storage of these key-value pairs: they are stored in the form of multiple files: /data/data/{packageName}/shared_prefs/{name}.xml. Where, the actual {name}.xml is specified in the program.
Then, we just call (name, mode) to get the SharedPreferences object mapped from the corresponding file in the packageName folder. Among them, mode is the way to read files.
Example:
1. The method of saving data using SharedPreferences is as follows:
//Instantiate SharedPreferences object (step 1)SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); //Instantiate the object (Step 2) editor = (); //Save data using putString method("name", "Karl"); ("habit", "sleep"); //Submit the current data(); //Use the toast information prompt box to prompt the successful writing of data(this, "The data was successfully written to SharedPreferences!" , Toast.LENGTH_LONG).show();
Execute the above code, SharedPreferences will save the data in a file. You can export the file under data/data/corresponding package name/ of File Explorer and view it.
2. The method of reading data using SharedPreferences is as follows:
//Similarly, a SharedPreferences object must be instantiated before reading SharedPreferences dataSharedPreferencessharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); // Use the getString method to get the value, note that the second parameter is the default value of the valueString name =("name", ""); String habit =("habit", ""); // Use the toast information prompt box to display information(this, "Read the data as follows:"+"\n"+"name:" + name + "\n" + "habit:" + habit, Toast.LENGTH_LONG).show();
3. Understand
We can simply understand it as: our customized XML files will be stored somewhere in the mobile phone, and each file will be used to store relevant key-value pair data. For example, if we want to store user preferences, we can create a SharedPreferences named UserPref and read and write.
4. Comparison of getSharedPreferenced and getPreference
The file obtained by getSharedPreferenced can be obtained using the context object in the package. You only need to specify the name to obtain a specific xml file.
However, the getPreference method is specially used within the Activity. The created XML file can only be obtained within the Activity context and does not require a name specified. This XML file only belongs to this Activity.
There is no difference between the others. The only difference is the difference between a permission domain.
Example:
Context context = getActivity(); SharedPreferences sharedPref = ("userPref", MODE_PRIVATE); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);