Android storage application scenarios and android storage scenarios

Source: Internet
Author: User

Android storage application scenarios and android storage scenarios

As a complete application, data storage operations are essential. Therefore, the Android system provides four data storage methods. They are SharePreference, file storage, SQLite, and Content Provider. The differences and application scenarios of these methods are as follows.
First: Use SharedPreferences to store data
Applicability: stores a small amount of data, and the format of the data is very simple: string type, basic type value. For example, various configuration information of the application (such as whether to enable sound effects, whether to use vibration effects, player points for small games, etc.), unlock ports to set passwords, etc.
Core Principle: stores key-value pairs stored in XML files, which are usually used to store some simple configuration information. Expand the File browsing tree through the File Explorer panel of DDMS. It is obvious that the SharedPreferences data is always stored in the/data // shared_prefs directory. The SharedPreferences object can only obtain data, but does not support storage and modification. The storage modification is implemented through the internal interface Editor object obtained by SharedPreferences. edit. SharedPreferences is an interface. A program cannot directly create a SharedPreferences instance. It can only obtain the SharedPreferences instance through the getSharedPreferences (String name, int mode) method provided by Context, in this method, name indicates the xml file name to be operated. The second parameter is as follows:
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: specify that the SharedPreferences data can be read and written by other applications.
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.
Type 2: file storage data
You can create a file to save data on the storage device of the device or an external storage device. By default, files cannot be shared among different programs.
Write File: Call the Context. openFileOutput () method to create a file based on the specified path and file name. This method returns a FileOutputStream object.
Read files: Call the Context. openFileInput () method to return a standard Java FileInputStream object through the specified path and file name.
Third: SQLite stores data
SQLite Database. Android supports databases very well. It is integrated with the SQLite database, and every application can easily use it. Or, more specifically, Android relies entirely on the SQLite database, all of its system data and structured data used are stored in the database. It has the following advantages: a. superior efficiency, which is undeniable B. It is very suitable for storing structured data c. It facilitates data transmission between different activities and even different applications.
Type 4: ContentProvider
A data storage method that can be shared by all applications in the Android system. Because data is usually private among applications, this storage method is rarely used, however, it is an essential storage method. For example, audio, video, image, and address book can be stored in this way. Each ContentProvider provides a public URI (encapsulated as a Uri object). If the application needs to share data, you need to use ContentProvider to define a URI for the data, then other applications pass in the URI through the Content Provider to operate the data.
To sum up, files are suitable for storing some simple text or binary data. SharedPreferences is suitable for storing some key-value pairs, while databases are suitable for those complex relational 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.