This article describes the usage of Perferences in Android programming. Share it for your reference, as follows:
When browsing the directory of each package in the /data/data/ directory of your mobile phone, you often see a shared_prefs folder with a package name_preferences.xml file. This file is the protagonist to be mentioned, and is referred to as the configuration file below;
This file is similar to a configuration file role, recording some attribute values of the application. For example, if your application provides a wizard function to guide users, it is estimated that it will definitely provide the option for the user to turn off this function. Then you can place this switch in this file and use this value to make the correct display when it is started next time;
The operation of this configuration file mainly uses two classes: PreferenceManager and SharedPreferences. SharedPreferences are used to operate the configuration file specifically, such as taking values from files, writing values to files, etc.; PreferenceManager is responsible for managing the configuration files of all applications in the system, and can use it to easily obtain the SharedPreferences object of the file through the application context (Content). It is managed uniformly by how to handle file paths, file names, etc., so you don’t have to worry about it when using it;
Here is a detailed explanation of how to use it:
1. Import package
import ; import ;
2. Get the object
3. Write/Update
editor = (); ("pre_key_words", true); ();
Of course, other types of data can also be written here, such as putInt and putString. You can view the methods in detail...
4. Get the value
It should be noted that "pre_key_words" sets the value of "pre_key_words" in the file, is it boolean or String? Don't use the wrong function when taking the value, otherwise it will be the second parameter (the default value) all the time;
In fact, SharedPreferences' reading and writing of package name_preferences.xml file looks very similar to VC and other ini configuration files~
I hope this article will be helpful to everyone's Android programming design.