Android data storage (1) -- shared preferences

Source: Internet
Author: User

Sharedpreferences indicates "Sharing Preferences"

It is shared by all activities and cannot share preferences outside the application package. Preferences are stored as a set of "keys/values. The supported data types are as follows:

1. Boolean Value

2. floating point value

3. integer value

4. long integer value

5. String Value

It is used as follows:

1. Create a sharedpreferences object instance

2. Create a sharedpreferences. Editor to modify the preference content.

3. Submit changes

Note: If you want to support a specific activity, you must specify the preference for obtaining the private mode. In this way, the activity in the application cannot be accessed.

The following are different method call preferences:

1. Obtain the activity's private preferences

Preferences = getpreferences (Mode_private);

Code snippet 9.1.1 activity private preference acquisition

2. Get application preferences

Sharedpreferences = getsharedpreferences ("sharedpreferences ",
Mode_private);

Code snippet 9.1.2 get application preferences

Note: both private and public preferences have the file storage mode, which allows you to perform different jobs on created preferences files.

1. Context. mode_private (0)

The default mode indicates that the file is private data and can only be accessed by the application. In this mode, if a file is written, the original file content will be overwritten.

2. Context. mode_append (32768)
Append mode. This mode checks whether the file exists. If yes, append the content to the file instead of overwriting it.

3. Context. mode_world_readable (1)

Indicates that the current file can be read by other applications.

4. Context. mode_world_writeable (2)

Indicates that the current file can be written by other applications.

Sharedpreferences contains some methods through which the stored content can be easily obtained.

Method

Description

Sharedpreferences. Contains ()

Check whether the preference exists by name

Sharedpreferences. Edit ()

Get Editor, mainly used to store information

Sharedpreferences. getall ()

Obtain all key-value pairs

Sharedpreferences. getboolean ()

Obtains a preference with a Boolean value.

Sharedpreferences. getfloat ()

Gets a preference with a value of float.

Sharedpreferences. getint ()

Gets a preference with a value of Int.

Sharedpreferences. getlong ()

Gets a preference with a long value.

Sharedpreferences. getstring ()

Gets a preference with a value of string.

Table 9.1.1 sharedpreferences

 

 

Through the sharedpreferences. Edit () method, we can get the editor, and the editor method is as follows:

Method

Description

Sharedpreferences. Editor. Clear ()

Remove all preferences. Note: This operation can be performed and submitted only after any modification.

Sharedpreferences. Editor. Remove ()

Remove a preference by name. The priority of this operation is as follows:

Sharedpreferences. Editor. putboolean ()

Preference for storing values as Boolean

Sharedpreferences. Editor. putfloat ()

Preference for storing a value of float

Sharedpreferences. Editor. putint ()

Preference for storing an int Value

Sharedpreferences. Editor. putlong ()

Preference for long storage

Sharedpreferences. Editor. putstring ()

Preference for storing values as string

Sharedpreferences. Editor. Commit ()

Submit all changes to this edit session

Table 9.1.2 sharedpreferences. Editor Method

The following is an example:

Androidstudy_sharepreferences

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.