Android data storage method-SharedPreferences, sharedpreferences

Source: Internet
Author: User

Android data storage method-SharedPreferences, sharedpreferences

Android data storage methods include SharedPreferences, file storage, SQLite database, Content provider, and network server.

This topic describes SharedPreferences.

For more information, see http://www.cnblogs.com/wuyudong/p/5939872.html.

SharedPreferences

How do I save software configuration parameters?

Window: using the INI File

J2SE application: Use the properties property File

Android: provides a SharedPreferences class, which is a lightweight storage class and is suitable for saving software configuration parameters.

Use SharedPreferences to save data. The data is stored in xml files, which are stored in the/data/<packagename>/shared_prefs directory.

SharedPreferences Interface

The SharedPreferences interface is mainly used to read the Preferences data of an application. It provides the following common methods to access the key_value key-value pair of SharedPreferences:

Method Name Description
Public abstract boolean contains (String key) Determine whether SharedPreferences contains data of a specific key
Public abstract SharedPreferences. Editor edit () Returns an Edit object used to operate SharedPreferences.
Public abstract Map <String,?> GetAll () Obtain all key-value pairs in the SharedPreferences data
GetXXX (String key, XXX defvlaue) Obtains the value corresponding to the specified key of the SharedPreferences data. If the key does not exist, the default value defValue is returned. XXX can be boolean, float, int, long, String, and other basic types of values.

Editor interface

SharedPreference is an interface that does not provide the ability to write or read data. However, there is an internal Editor interface, which has a series of methods for operating SharedPreference.

Method Name Description
Public abstract SharedPreferences. Editor clear () Clear all data in SharedPreferences
Public abstract boolean commit () After the Editor is edited, you can call this method to submit modifications. You must call this data to modify the changes.
Public abstract SharedPreferences. Editor putXXX (String key, boolean XXX) Store the data corresponding to the specified key to SharedPreferences. XXX can be a value of basic types such as boolean, float, int, long, and String.
Public abstract SharedPreferences. Editor remove (String key) Delete the data item corresponding to the specified key in SharedPreferences

SharedPreferences

SharedPreferences sharedPreferences = getSharedPreferences ("wyd001", Context. MODE_PRIVATE); Editor editor = sharedPreferences. edit (); // get the editor. putString ("name", "wyd"); editor. putInt ("age", 22); editor. commit (); // submit the modification

The generated wyd001.xml file is as follows:

<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?><map>    <string name="name">wyd</string>    <int name="age" value=“22" /></map>

SharedPreferencesOperation Mode

GetSharedPreferences (na, memode) Method

Parameter 1: Specifies the file name without a suffix.

Parameter 2: Specifies the file operation mode. There are four operation modes.

Context. MODE_PRIVATE:The default operation mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use Context. MODE_APPEND.

Context. MODE_APPEND:Mode: Check whether the file exists. If yes, append the content to the file. Otherwise, create a new file.

Context. MODE_WORLD_READABLEAndContext. MODE_WORLD_WRITEABLEUsed to control whether other applications have the permission to read and write the file.

MODE_WORLD_READABLE:Indicates that the current file can be read by other applications.;MODE_WORLD_WRITEABLE:Indicates that the current file can be written by other applications.

The getPreferences (mode) method is used to operate SharedPreferences. By default, the Class Name of the current class without the package name is used as the file name.

AccessSharedPreferencesData

The data code used to access SharedPreferences is as follows:

SharedPreferences sharedPreferences = getSharedPreferences ("wyd001", Context. MODE_PRIVATE); // getString () The second parameter is the default value. If the key does not exist in the preference, the default value String name = sharedPreferences is returned. getString ("name", ""); int age = sharedPreferences. getInt ("age", 1 );

 

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.