Four storage methods of Android data: sharedpreferences, SQLite, content provider, and file sharepreferences

Source: Internet
Author: User
In addition to the SQLite database, sharedpreferences is also a lightweight data storage method. In essence, sharedpreferences stores key-value pairs based on XML files and is usually used to store some simple configuration information. Its storage location is in the/data/<package name>/shared_prefs directory. The sharedpreferences object can only obtain data, but does not support storage and modification. The storage modification is implemented through the editor object. To store sharedpreferences, follow these steps:

1. Get the sharedpreferences object based on Context

2. Use the Edit () method to obtain the editor object.

3. Use the editor object to store key-value pairs.

4. submit data using the Commit () method.

ImplementationCodeAs follows:

 1 Public ClassMainactivityExtends Activity {
2 @ Override
3 Public Void 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 //Store Data
11 Editor= Sp. Edit ();
12 Editor. putstring ("String_key","String" );
13 Editor. putint ("Int_key",0 );
14 Editor. putboolean ("Boolean_key",True );
15 Editor. Commit ();
16
17 //Returns the string_key value.
18 Log. D ("SP", Sp. getstring ("String_key","None" ));
19 //If not_exist does not exist, the returned value is "NONE"
20 Log. D ("SP", Sp. getstring ("Not_exist","None" ));
21 }
22 }

After the code is executed, an SP. xml file is generated under the/data/COM. Test/shared_prefs directory. An application can create multiple such XML files. :

The specific content of the SP. xml file is 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 >

InProgramIn the code, the getxxx method is used to conveniently obtain the value of the corresponding key. If the key value is incorrect or the key does not have the corresponding value, sharedpreferences provides an opportunity to assign the default value, to ensure program robustness. If there is no key with the value "not_exist" in the running result, log prints the default value: "NONE ". During access to a non-key value, no exception is thrown.

Compared with SQLite databases, sharedpreferences removes the need to create databases, create tables, write SQL statements, and perform other operations, making sharedpreferences more convenient and concise. However, sharedpreferences also has its own defects, such as its function storage Boolean, Int, float, long, and string simple data types, such as its inability to perform conditional queries. Therefore, no matter how simple the sharedpreferences data storage operation is, it can only be a supplement to the storage method, and cannot completely replace other data storage methods such as SQLite database.

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.