Browse the package directories in the/data/directory of the mobile phone. You will often see a shared_prefs folder withPackage name_preferences. xmlFile. This file is the main character;
This file is similar to the role of a configuration file and records some attribute values of the application. For example, if your application provides the wizard function to guide users, it is estimated that the option will be provided for the user to disable this function, so you can place this switch in this file, and the value will be correctly displayed at the next startup;
The operation of this configuration file mainly uses two classes: preferencemanager and sharedpreferences. sharedpreferences is used to perform specific operations on this configuration file, such as taking values from the file and writing values to the file; preferencemanager is responsible for managing the configuration files of all applications in the system. It can be used to easily obtain the sharedpreferences object of the file through the application context (content) and how to process the file path, the file names are all managed in a unified manner, so you don't need to worry about them when using them;
The following describes how to use it:
1. Import package
ImportAndroid. content. sharedpreferences;ImportAndroid. Preference. preferencemanager;
2. Get the object
Sharedpreferences mprefs = preferencemanager. getdefasharsharedpreferences (This);
3. Write/update
Sharedpreferences. Editor editor = mprefs. Edit (); editor. putboolean ("Pre_key_words",True); Editor. Commit ();
Of course, other types of data can be written here, such as putint and putstring. For details, refer to the methods in sharedpreferences. Java...
4. Values
BooleanCheckedkeywords = mprefs. getboolean ("Pre_key_words",False);
Note that"Pre_key_words"What values are set in the file, Boolean or string? Do not use the wrong function when setting the value; otherwise, the second parameter (default value) will always be returned;
In fact, sharedpreferencesPackage name_preferences. xmlReading and Writing files looks like reading and writing ini configuration files like VC ~