Android Data Storage--sharedpreferences Storage

Source: Internet
Author: User

Sharedpreferences is also a lightweight method of data storage, which is essentially based on XML files that store Key-value key-value pairs of data , typically used to store some simple configuration information. Its storage location is under the /data/data/< package name >/shared_prefs directory. Read Sharedpreference1. Get the sharedpreference object     /*
* Name Open sharedpreferences file name (No. xml suffix required)
* Mode Open Sharedpreferences
*/
Sharedpreferences getsharedpreferences (String name, int mode) Four types of sharedpreferences acquired patterns context.mode_private = 0
Context.mode_world_readable = 1
Context.mode_world_writeable = 2
Context. Mode_multi_process = 4 Mode_private is the default mode of operation, which means that the file is private and can only be accessed by the app itself, and the content written in that mode overwrites the contents of the original file. mode_world_readable indicates that the current file can be read by another application. mode_world_writeable indicates that the current file can be written by another application. mode_multi_process sharedpreference The load tag, set it up, the file will be checked when the Sharedpreference instance is loaded into the process, and is mainly used in cases where there are multiple processes in an application. 2. Reading datause Sharedpreference's getxxx () to obtain the appropriate data, mainly including: boolean   Getboolean (String key,  boolean  defvalue)
float  getfloat (String key,  Float  defvalue)
int  getint (String key,  int  defvalue)
long  getlong (String key,  long   Defvalue)
String getString (String key, String defvalue)
Set< string< Span style= "COLOR: #333399" >>  getstringset (String key, Set< String>  defvalues)
Write sharedpreference It is important to note that the Sharedpreferences object itself can only fetch data without supporting storage and modification, and the storage modifications are implemented through the editor object . the steps to implement Sharedpreferences storage are as follows:1. Get Sharedpreferences objects based on context Get sharedpreferences object step with Read Sharedpreferences2. Use the edit () method to get the editor object. through sharedpreferences. edit () gets Editor Object 3. Store the Key-value key value pair data through the editor object. Putboolean (String key, boolean value)
Putfloat (String key, float value)
Putint (String key, int value)
Putlong (String key, long value)
Putstring (string key, String value)
Putstringset (String key, Set<string> values)Tips: Here's a question, Putxxx's return value is Sharedpreferences.editor object, Android developer above says:Returns
    • Returns a reference to the same Editor object, so you can chain put calls together.
He is puzzled and asks the great God to explain. 4. Submit the data through the commit () method. /*
* Return if the new data is successfully submitted returns true conversely false
*/
Boolean commit ()
Tips: If you want the XML file behind sharedpreferences to be read and written by other applications, you can specify context.mode_world_readable and Context.mode_world_ Writeable permissions. However, mode_world_writeable permissions are used in practice, and other applications cannot modify the data of this program. If other programs want to modify this program data, you can use Android:shareduserid in the configuration file.

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.