Application scenarios for the Android storage method

Source: Internet
Author: User
Tags sqlite database java fileinputstream

as a complete application, data storage operations are essential. As a result, the Android system offers four ways to store data. respectively: sharepreference, file storage, SQLite, Content Provider. The different and application scenarios for these methods are summarized below.
First: Use Sharedpreferences to store data
Scope of application: Save a small amount of data, and the format of the data is very simple: string type, basic type of value. such as the application of various configuration information (such as whether to open the sound effects, whether to use vibration effects, game players points, etc.), unlock password password, etc.
Core principle: Save Key-value Key-value pairs of data based on XML file storage, usually used to store some simple configuration information. With Ddms's File Explorer panel, it is clear that sharedpreferences data is always stored in the/data/data//shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and the storage modification is achieved through the internal Interface Editor object obtained by Sharedpreferences.edit (). The sharedpreferences itself is an interface, and the program cannot create the Sharedpreferences instance directly, only through the getsharedpreferences provided by the context (String name, int mode) method to get the Sharedpreferences instance, in which name represents the XML file name to manipulate, and the second parameter is as follows:
context.mode_private: Specifies that the sharedpreferences data can only be read and written by the application.
context.mode_world_readable: Specifies that the sharedpreferences data can be read by other applications, but cannot be written.
context.mode_world_writeable: Specifies that the sharedpreferences data can be read by other applications, write
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.
The second type: file storage data
You can create a file to hold data on the device's own storage device or on an external storage device. Also in the default state, files cannot be shared between 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 file: Call the Context.openfileinput () method to return a standard Java FileInputStream object by developing the path and file name.
The third type: SQLite stores data
SQLite database. Android supports the database very well, it itself integrates the SQLite database, each application can easily use it, or more specifically, Android relies entirely on the SQLite database, all of its system data and the use of structured data are stored in the database.  It has the following advantages: A. Excellent efficiency, which is undeniable B. Ideal for storing structured data c. Facilitates the transfer of data between different activity and even different applications.
Fourth type: ContentProvider
a data storage method that enables all applications to be shared in an Android system, because the data is usually private to each application, so this storage method is less used, but it is an essential storage method. For example, audio, video, pictures, and contacts can generally be stored in this way. Each contentprovider provides a public URI (wrapped as a URI object), and if the application has data to be shared, it needs to use ContentProvider to define a URI for the data, and then the other application passes the content Provider pass in the URI to manipulate the data.
To summarize, the file is suitable for storing some simple text data or binary data, sharedpreferences is suitable for storing some key-value pairs, and the database is suitable for those complex relational data.

Application scenarios for the Android storage method

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.