Android data storage (1) shared preferences data storage

Source: Internet
Author: User

Android provides four data storage methods. However, because the stored data is private to its applications, if you need to use the data in other applications, you must use the content provider (Data Sharing) provided by Android ).

The official documentation has a detailed description: http://developer.android.com/guide/topics/data/data-storage.html

The four data storage methods in Android are as follows.

1. Shared preferences: used to store data in the "key-Value Pair res" format. It is a lightweight key-value storage mechanism that can only store basic data types.

2. Files: It operates files through fileinputstream and fileoutputstream. However, in Android, files are private to one application, and one application cannot read or write files from other applications.

3. SQLite: a standard database provided by android that supports SQL statements

4. Network: stores and obtains data through the network.


1. Shared preferencesData Storage

Shared preferences is mainly used to save system configuration information. For example, you have set a sound effect for the program interface and want to retain the sound effects set last time at the next startup. Because the interface of the Android system is in the form of an activity stack, some interfaces will be reclaimed when system resources are insufficient. Therefore, some operations need to be preserved when no activity occurs, so that it can be displayed when it is activated again.

Sharedpreference can only store basic data types.

There are two methods to obtain the sharedpreference object: context instances, such as activity, and the following method is called:

Getsharedpreference (): To obtain the sharedpreference object in this way, you must specify the name of the preference file in the first parameter of this method.

Getpreference (); in this way, you can obtain a unique preference file for your activity without specifying a name.

When obtaining a sharedpreference object through these two methods, you must specify the Read mode of the preference file. There are three modes (not four ):

Public static final int mode_private = 0x0000; indicates that only this program can access

Public static final int mode_world_readable = 0x0001; indicates that other programs can only perform read operations.

Public static final int mode_world_writeable = 0x0002; indicates that other programs can perform read and write operations.

Write Data to the preference file:

1. Call the Edit () method to obtain a sharedpreference. Editor object.

2. Call putboolean (), putstring (), and other put... () Methods to store data.

3. Call the Commit () method to submit data. Because the put... () method, the data is first stored in the memory, and the Commit () method is called to store the data in the preference file. The advantage of this is that you do not have to store clothes or data, only one operation is performed on the file to save memory overhead.

Read data from preference files:

Call the getboolean, getstring (), and other get... () Methods of the sharedpreference object to read data.

Example:

Public class calc extends activity {public static final string prefs_name = "myprefsfile"; @ override protected void oncreate (bundle state) {super. oncreate (State );... // read Data Restore preferences sharedpreferences settings = getsharedpreferences (prefs_name, mode_private); Boolean silent = settings. getboolean ("silentmode", false); setsilent (silent) ;}@ override protected void onstop () {super. onstop (); // store data we need an editor object to make preference changes. // all objects are from Android. context. context sharedpreferences settings = getsharedpreferences (prefs_name, 0); sharedpreferences. editor editor = settings. edit (); Editor. putboolean ("silentmode", msilentmode); // do not forget to submit commit () Editor. commit ();}}

We know that sharedpreference is stored as a key-value pair. From the get and put methods, we can see that the preference file itself is an XML file. You can just open it and see it.

The preference files generated in this way all have a fixed location:

File Explorer/data/package name/shared_prefs/file name. xml

 

Call the sharedpreference file of other applications

To read from other applications, the preference file of the other application must be readable or readable.

Steps:

1. You need to create the context corresponding to other applications, such

Context context=createPackageContext("com.tao.androidtest", Context.CONTEXT_IGNORE_SECURITY);   

The first parameter is the package name of another application. In fact, the android system uses the package name of the application as the flag of the program.

The second parameter is the flags. It has two options: context_include_code and context_ignore_security. Context_include_code indicates that the code in this package can be executed. Context_ignore_security indicates that the security warning is ignored. If this flag is not added, some functions cannot be used and a security warning will appear.

2. You can use the getsahredpreference () method of the context of other applications to obtain the corresponding sahredpreference object.

Only when the mode of the preference file of the other application is set to mode_world_readable or mode_world_readable + mode_world_writeable can the file be accessed by the external application.

Then, you can use the same sahredpreference object in this program.

Of course, the above operations are performed through the API provided by Android. You can also build a file by yourself, and the XML file in the parsing is being operated, but this method is more complicated.

File file = new file ("Data/data/COM. Tao. androidtest/shard_prefs/test. xml ");

Obtain the file, and then parse the XML file. We will introduce how to parse the XML file later.

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.