Android data storage-SharedPreferences Storage

Source: Internet
Author: User

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 ModesContext. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.