Usage of sharedpreferences in Android

Source: Internet
Author: User

Original address: http://blog.sina.com.cn/s/blog_7ab107b00100u78t.html sharedpreferences Introduction:

Do software development should know, a lot of software will have configuration files, which is stored in the program running the various property values, because of its configuration information is not much, if the use of database to store is not cost-effective, because the database connection and operation time greatly affected the efficiency of the program, So we use the corresponding relationship of the key value to store these configuration information. Sharedpreferences is the technology used in Android to implement this storage method.

The sharedpreferences is simple to use and can easily store data and read data. Sharedpreferences can save only simple types of data, such as String, int, and so on. It is common to convert data of complex types to Base64 encoding, then save the converted data as a string in an XML file, and then save it with Sharedpreferences.

The steps to save the Key-value pair using Sharedpreferences are as follows:

(1) Use the Getsharedpreferences method of the activity class to get the Sharedpreferences object, where the name of the file that stores the Key-value is specified by the first parameter of the Getsharedpreferences method.

(2) Use edit of Sharedpreferences interface to get Sharedpreferences.editor object.

(3) The Putxxx method of Sharedpreferences.editor interface is used to save the key-value pair. where xxx represents a different data type. For example, the value of string type requires the Putstring method.

(4) The Key-value pair is saved by the commit method of the Sharedpreferences.editor interface. The Commit method is equivalent to a commit operation in a database transaction.

The writing process for the specific code is:

A, storage data information

1. Open preferences, name is setting, if present, open it, otherwise create a new preferences

Sharedpreferences settings = getsharedpreferences ("setting", 0);

2, let setting in the editing state

Sharedpreferences.editor Editor = Settings.edit ();

3. Store data

Editor.putstring ("name", "Ataaw");
Editor.putstring ("URL", "ataaw.com");

4. Complete the submission

Editor.commit ();

B, read the data information

1. Get Preferences

Sharedpreferences settings = getsharedpreferences ("setting", 0);

2. Take out the data

String name = settings.getstring ("name", "Default value");
String url = setting.getstring ("url", "Default");

This is how sharedpreferences is used in Android, where the preferences file location you create can be viewed in eclipse:

Ddms->file Explorer/<package Name>/shared_prefs/setting.xml

The main code is specific as follows (for reference only):

Call the Restoreprefs () method in the OnCreate method:

Implementation of the Read function
private void Restoreprefs () {
TODO auto-generated Method Stub
Sharedpreferences is a lightweight way of storing data. is a key-value pair to store the application's
A way to configure information that can only store basic data types.
Declares a priority setting type settings, loading the configuration file


Sharedpreferences settings = getpreferences (BMI. Mode_private);

Use the GETXXX function to read different types of content from the priority setting to get the content of the priority setting parameters


String pref_height = settings.getstring (Pref_height, "");

This sentence is a wake-up memory statement


Field_height.settext (Pref_height);
Field_weight.requestfocus ()///Set focus field to the weight field you just need to enter the weight number directly.

}

stored in the activity every time it dims, that is, when leaving the current screen, remember to set the height value is the storage priority setting
protected void OnPause ()
{
Super.onpause ();
LOG.V ("BMI", "onPause1");
Get Sharedpreferences Object


Sharedpreferences settings = getpreferences (BMI. Mode_private);


Get Sharedpreferences.editor


Sharedpreferences.editor Editor = Settings.edit ();


Save component values for priority settings fill in different types of content
Use the "Field_height" interface component identifier to get the height of the string and then save it to the priority set parameter represented by Pref_height


Editor.putstring (Pref_height, Field_height.gettext (). toString ());


The result of committing the save will be changed to write to the system


Editor.commit ();
}

Usage of sharedpreferences in Android

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.