Four ways to store Android data sharedpreferences, SQLite, Content provider, and file (iii)--sharepreferences

Source: Internet
Author: User

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:

1 Publicclass mainactivity extends Activity {
2 @Override
3 publicvoid onCreate (Bundle savedinstancestate) {
4 super.oncreate (savedinstancestate);
5 Setcontentview (R.layout.main);
6
7//Get Sharedpreferences Object
8 Context ctx = mainactivity.this;
9 Sharedpreferences sp = ctx.getsharedpreferences ("sp", mode_private);
10//Deposit data
Editor editor = Sp.edit ();
editor.putstring ("String_key", "STRING");
editor.putint ("Int_key", 0);
Editor.putboolean ("Boolean_key", true);
Editor.commit ();
16
17//return value of String_key
log.d ("SP", Sp.getstring ("String_key", "none"));
19//If Not_exist does not exist, the return value is "none"
log.d ("SP", Sp.getstring ("Not_exist", "none"));
+ }
22}

After this code is executed, a sp.xml file is generated in the/data/data/com.test/shared_prefs directory, and an application can create multiple such XML files. :

The specific contents of the Sp.xml file are as follows:

1 <?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
2 <map>
3 <string name= "String_key" >string</string>
4 <int name= "Int_key" value= "0"/>
5 <boolean name= "Boolean_key" value= "true"/>
6 </map>

In the program code, through the GetXXX method, you can easily get the value of the corresponding key, if the key value is wrong or the key has no corresponding value, Sharedpreferences provides an opportunity to give the default value, in order to ensure the robustness of the program. As the result of the operation, because there is no value of "not_exist" key, so log prints its default value: "None". There is no exception thrown in the process of accessing a key value that does not exist.

Compared with SQLite database, Sharedpreferences object is more convenient and concise than creating database, creating table, writing SQL statement and so on. But Sharedpreferences also has its own flaws, such as its functional storage boolean,int,float,long and string five of simple data types, such as its inability to perform conditional queries. So no matter how simple the sharedpreferences data store operation is, it can only be a supplement to the storage method, and it cannot completely replace other data storage methods such as SQLite database.

RELATED links:

Four ways to store Android data sharedpreferences, SQLite, Content provider, and file (a)--overview

Four ways to store Android data sharedpreferences, SQLite, Content provider, and file (ii)--sqlite

Four ways to store Android data sharedpreferences, SQLite, Content provider, and file (iv)--contentprovider

Four ways to store Android data sharedpreferences, SQLite, Content provider, and file (iii)--sharepreferences

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.