In addition to the SQLite database, Sharedpreferences is also a lightweight way of storing data, which is essentially based on XML files that store Key-value key-value pairs of data and are typically used to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object. The steps to implement Sharedpreferences storage are as follows:
First, according to the context to obtain Sharedpreferences object
Second, use the edit () method to get the editor object.
Third, through the editor object store Key-value key value pair data.
Iv. submit data through the commit () method.
The specific implementation code is as follows:
public class Mainactivity extends Activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get Sharedpreferences Object
Context CTX = mainactivity.this;
Sharedpreferences sp = ctx.getsharedpreferences ("sp", mode_private);
Deposit data
Editor editor = Sp.edit ();
Editor.putstring ("String_key", "STRING");
Editor.putint ("Int_key", 0);
Editor.putboolean ("Boolean_key", true);
Editor.commit ();
Returns the value of the String_key
LOG.D ("SP", Sp.getstring ("String_key", "none"));
If Not_exist does not exist, the return value is "none"
LOG.D ("SP", Sp.getstring ("Not_exist", "none"));
}
}
Sharedpreferences commonly used properties and methods
Method name |
Describe |
Public abstract Boolean contains (String key) |
Determine if Sharedpreferences contains data for a specific key |
Public abstract Sharedpreferences.editor Edit () |
Returns an edit object for Operation Sharedpreferences |
Public abstract map<string,?> getAll () |
Get all the Key-value in the sharedpreferences data. |
GetXXX (String key,xxx Defvlaue) |
Gets the value of the sharedpreferences data specified by key, and returns the default value of Defvalue if the key does not exist. where xxx can be a Boolean, float, int, long, string, and other basic types of values |
Since Sharedpreference is an interface, it does not provide the ability to write data and read data in this interface. But inside it there is an interface inside the editor, and the edit interface has a series of methods for manipulating sharedpreference.
Common Methods of editor interface
Method name |
Describe |
Public abstract Sharedpreferences.editor Clear () |
Empty all the data in the sharedpreferences. |
Public abstract Boolean commit () |
When editor is finished, call this method to commit the modification, and you must call this data to modify |
Public abstract Sharedpreferences.editor putxxx (String key, Boolean XXX) |
The data corresponding to the specified key is deposited to the sharedpreferences, where xxx can be a Boolean, float, int, long, string, and other basic types of values |
Public abstract Sharedpreferences.editor Remove (String key) |
Delete the data item corresponding to the specified key in the Sharedpreferences |
Sharedpreferences is an interface where programs cannot create sharedpreferences instances, and can be context.getsharedpreferences (String name,int mode) to get a sharedpreferences instance
Name: Refers to the file name, does not need to add the suffix. XML, the system will be added to us automatically. Generally this file is stored under /data/data/<package name>/shared_prefs (often asked in this interview)
Mode: is the specified read and write mode, the value of three kinds, respectively:
context.mode_private: Specifies that the sharedpreferences data can only be read and written by this application
context.mode_world_readable: Specifies that the sharedpreferences data can be read by other applications but cannot be written
context.mode_world_writeable: Specifies that the sharedpreferences data can be read and written by other applications.
More information can be viewed: http://blog.sina.com.cn/s/blog_4f1c99de0101hur1.html
Android Development Note "Sharepreference data access"