SharedPreferences is also a lightweight data storage method.The essence is to store key-value pairs of data based on XML files.Is usually used to store some simple configuration information. Its storage location is/Data/ <包名> /Shared_prefs Directory. Read SharedPreference 1. Get SharedPreference object
/*
* Name: name of the SharedPreferences file opened by name (. xml suffix is not required)
* SharedPreferences mode enabled by mode
*/
SharedPreferences getSharedPreferences (String name,IntMode) Four SharedPreferences obtaining Modes
Context. MODE_PRIVATE = 0
Context. MODE_WORLD_READABLE = 1
Context. MODE_WORLD_WRITEABLE = 2
Context. MODE_MULTI_PROCESS = 4MODE_PRIVATE is the default operation mode. This indicates that the file is private data and can only be accessed by the application. In this mode, the content written will overwrite the content of the original file. MODE_WORLD_READABLE indicates that the current file can be read by other applications. MODE_WORLD_WRITEABLE indicates that the current file can be written by other applications. MODE_MULTI_PROCESS: Specifies the SharedPreference loading tag. When this parameter is set, the file is checked whether the SharedPreference instance is modified when it is loaded to the process. This is mainly used when an application has multiple processes. 2. Read the data and use getXXX () of SharedPreference to obtain the corresponding data, including:
BooleanGetBoolean (String key,BooleanDefValue)
FloatGetFloat (String key,FloatDefValue)
IntGetInt (String key,IntDefValue)
LongGetLong (String key,LongDefValue)
String getString (String key, String defValue)
Set GetStringSet (String key, Set DefValues)
Note that the SharedPreferences object itself can only obtain data and does not support storage and modification, Storage modification is implemented through the Editor object. To store SharedPreferences, perform the following steps: 1. Obtain the SharedPreferences object based on Context and read the SharedPreferences object; 2. Obtain the Editor object using the edit () method. Use SharedPreferences. edit () to obtain the Editor object. 3. Use the Editor object to store key-value pairs.
PutBoolean (String key, BooleanValue)
PutFloat (String key, FloatValue)
PutInt (String key, IntValue)
PutLong (String key, LongValue)
PutString (String key, String value)
PutStringSet (String key, Set Values) Tips: there is a problem here. The putXXX return value is the SharedPreferences. Editor object. What Android Developer said above is: Returns
- Returns a reference to the same Editor object, so you can chain put calltogether. It's hard to understand. 4. submit data using the commit () method.
/*
* Return true if new data is successfully submitted; otherwise false
*/
BooleanCommit ()
Tips: If you want the xml file used by SharedPreferences to be read and written by other applications, you can specify the Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE permissions. However, the MODE_WORLD_WRITEABLE permission is actually used, and other applications cannot modify the data of this program. If other programs want to modify the program data, you can use android: sharedUserId in the configuration file.