Android local storage solution SharedPreferences

Source: Internet
Author: User

Android local storage solution SharedPreferences


Original article address
Storage location

The SharedPreferences data is stored in:/Data/ /Shared_prefs Directory, saved in XML format, the root element is:. The file name is the parameter value passed when obtaining the SharedPreferences instance.


   
   
     value 
   


Get instance

SharePerferences is an interface. Context implements the API: getSharedPreferences (String, int). This API returns a SharePerferences instance based on the String. The SharePerferences instances returned by the same String are the same. Strign specifies the name of the xml file that stores data.

Activity implements getPreferences (int). By default, the class name of Activity is used as the String to call getSharedPreferences (String, int ).


// Specify that the SharedPreferences data can only be read and written by this application. Context. MODE_PRIVATE // specify that the SharedPreferences data can also be read by other applications, but cannot be written. Context. MODE_WORLD_READABLE // specify that the SharedPreferences data can also be read and written by other applications. Context. MODE_WORLD_WRITEABLE // check whether the file exists. If so, append the file. Otherwise, create Context. MODE_WORLD_APPEND.


Read

// Determine whether SharedPreferences contains data of a specific key. Boolean contains (String key) // obtain all key-value pairs in SharedPreferences. Map
  
   
GetAll () // obtain the value corresponding to the specified key. If the key does not exist, the default value defValue is returned. Xxx getXxx (String key, Xxx defValue)
  


Write

The SharedPreferences interface does not provide the ability to write data, but uses its internal interface. You can call the SharedPreferences. edit () method to obtain the corresponding SharedPreferences. Editor object. Editor can write data to SharedPreferences as follows:


// Clear all data in SharedPreferences. Editor. clear () // store the data of the specified key to SharedPreferences. Editor. putXxx (String key, Xxx Value) // Delete the data item corresponding to the specified key in SharedPreferences. Editor. remove (String key) // when the Editor is edited, call this method to submit the modification. Boolean Editor. commit ()


Read and Write SharedPreferences of other applications

1. Create the corresponding Context of other applications.

Context useContext =createPackageContext("package_name",Context.CONTEXT_IGNORE_SECURITY);

2. Call the getSharedPreferences () method of the Context of other applications to obtain the SharedPreferences object.

3. Call the SharedPreferences. edit () method of other applications to obtain the corresponding SharedPreferences. Editor object.



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.