Five ways of storing Android data

Source: Internet
Author: User

1. Overview

Android provides 5 ways for users to save persisted application data. Choose according to your needs, such as whether the data is private to the application, whether it can be accessed by other programs, how much data storage space is required, and so on, respectively:
  
① using sharedpreferences to store data

② File Store data

③sqlite Database Storage Data

④ using ContentProvider to store data

⑤ Networked Storage data

Android provides a way to expose your data (even private data) to other applications-ContentProvider. It is an optional component that can read and write your application data publicly.

2. Sharedpreferences Storage

The Sharedpreference class provides a general framework that enables you to save and retrieve persistent key-value pairs for any base data type (Boolean, float, int, long, String) ("Key-value" based on XML file storage Key-value pair data).

Typically used to store some configuration information for a program. It is stored under the data/data/package name/shared_prefs directory.

XML processing is Dalvik through the native XML parser, such as the Xmlpull method, which is better for memory resource consumption.

2.1 We can get the Sharedpreferences object (via context) in the following two ways:

①getsharedpreferences (String name, int mode)

When we have multiple sharedpreferences, the corresponding Sharedpreferences object is obtained according to the first parameter name.

②getpreferences (int mode)

Use if you only need one sharedpreferences in your activity.

The mode here has four options:

Context.mode_private

The sharedpreferences data can only be read and written by the application.

Context.mode_world_readable

The sharedpreferences data can be read by other applications, but cannot be written.

Context.mode_world_writeable

The sharedpreferences data can be read and written by other applications.

Context.mode_multi_process

The option added after sdk2.3, when multiple processes read and write to the same sharedpreferences, it checks to see if the file is modified.

2.2 writing values to shared preferences
 
First, through the sharedpreferences.editor to get to the editor object;

Then the value is deposited by means of putboolean () or putstring () of editor.

Finally call Editor's commit () method to commit;

sharedpreferences settings = getsharedpreferences ("Fanrunqi", 0= Settings.edit ();   Editor.putboolean (true//  submit this edit Editor.commit ();

There are also two common ways to edit:

Editor.remove (String key): The next commit will remove key values corresponding to Editor.clear (): Remove all key-value pairs

2.3 reading values from shared preferences

Reading the value using the Sharedpreference object's Getboolean () or GetString () method is ok (no editor).

Sharedpreferences settings = getsharedpreferences ("Fanrunqi", 0); boolean isamazing= settings.getboolean ("Isamazing",true);

2.4 The pros and cons of Shared preferences

It can be seen that preferences is a very lightweight application, it is also very convenient to use, concise. However, the storage data type is relatively single (only the basic data type), can not be conditional query, can only be used under the non-complex storage requirements, such as saving configuration information.

Five ways of storing Android data

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.