Usage of sharedpreferences in Android

Source: Internet
Author: User

1.SharedpreferencesIntroduction

To save software configuration parameters, the Android platform provides a sharedpreferences class, which is a lightweight storage class and is especially suitable for saving software configuration parameters. Use sharedpreferences to save data. xml files are used to store data in the/data/<package name>/shared_prefs directory.

2.ObtainSharedpreferencesObject method:

SharedPreferences pre = Context.getSharedPreferences(String name,int mode);

Note: Name is the configuration file name of this component. (If you want to share this configuration file with other components of this application, you can use this name to retrieve this configuration file. Pay special attention here, because sharedpreferences are saved in XML format in Android, do not specify ". XML suffix. Android will automatically add it. You only need to write the file name directly. It is directly saved in the/data/<package name>/shared_prefs path, and saved as a key-value pair. When you need to obtain a parameter value, you can index it according to the key of the parameter ).

SharedPreferences pre = Activity.getPreferences(int mode);

Note: The configuration file can only be used by called activity. Mode is the operation mode. The default mode is 0 or mode_private. You can also use mode_append, mode_world_readable, and mode_world_writeable.

SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(Context);

Note: Each application has a default configuration file.Preferences. xml, UseGetdefasharsharedpreferences.

3. sharedpreferencesProcedure:

Sharedpreferences is easy to use and can easily store and read data. Sharedpreferences can only store simple data types, such as string and Int. Generally, the data of complex types is converted to base64 encoding, And the converted data is saved as a string in the XML file, and then saved in sharedpreferences.

UseSharedpreferencesSaveKey-ValuePerform the following steps:

1) Get the sharedpreferences object

2) obtain the sharedpreferences. Editor object.

3) use the putxxx () method of the sharedpreferences. Editor interface to store key-value pairs (xxx indicates different data types. For example, the putstring () method is required for string-type values ).

4) use the Commit () method of the sharedpreferences. Editor interface to save the key-Value Pair (the commit method is equivalent to the Commit (COMMIT) operation in the database transaction ).

4.Data storage and data reading process:

Store Data Information

1) Open the configuration file named configuration. If yes, open it. Otherwise, create a new configuration file named configuration.

Sharedpreferences = getsharedpreferences ("configuration", 0 );

2) Make sharedpreferences editable

Sharedpreferences. Editor editor = sharedpreferences. Edit ();

3) store data

Editor. putstring ("name", "Harvey ");

4) Submit

Editor. Commit ();

Read data information

1) Open the configuration file named configuration.

Sharedpreferences = getsharedpreferences ("configuration", 0 );

2) Get Data

String name = sharedpreferences. getstring ("name", "default ");

The above is how to use sharedpreferences in Android. The storage location of the created configuration file can be viewed in Eclipse:

Ddms --- file explorer --- data/<package name>/shared_prefs/configuration. xml

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.