The differences and advantages of V storage in Android

Source: Internet
Author: User
Tags sqlite sqlite database
Data storage is the most frequently used in development, where the 5 main ways to implement data storage in the Android platform are: 1 using sharedpreferences storage data 2 file storage data 3 SQLite database storage data 4 Using ContentProvider to store data 5 networked storage data first: Using Sharedpreferences to store data

Scope of application: Save a small amount of data, and the format of the data is very simple: string type, the value of the base type. such as the application of various configuration information (such as whether to open the sound, whether the use of vibration effects, games, players, etc.), unlock password password, etc.

Core principle: Its essence is based on XML file storage Key-value key value pairs of data, usually used to store some simple configuration information. Its storage location is in the/data/data/< >/shared_prefs directory.
The sharedpreferences itself is an interface in which a program cannot directly create Sharedpreferences instances, only getsharedpreferences provided through the context (String name, int mode) method to obtain the Sharedpreferences instance in which the K value name represents the XML file name to be manipulated, and the second parameter key value is as follows:

The steps for implementing sharedpreferences storage are as follows: First, get the Sharedpreferences object according to the context two, use the edit () method to get the editor object. Store Key-value key value pairs through the editor object. Submission of data through the commit () method.

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 another application, but not written.

Context.mode_world_writeable: Specifies that the sharedpreferences data can be read and written by other applications

Compared with the SQLite database, sharedpreferences is more convenient and concise than creating databases, creating tables, writing SQL statements, and many other operations. But Sharedpreferences also has its own flaws, such as it can only store Boolean,int,float,long and string five kinds of simple data types, such as its inability to make conditional queries. So no matter how simple the sharedpreferences data store operation is, it can only be a supplement to the way it is stored, and it cannot completely replace other data storage methods such as the SQLite database.

Second: File storage data about file storage, Activity provides Openfileoutput () method can be used to output data to a file, the specific implementation process and in the J2SE environment to save data to the file is the same. Files can be used to store large amounts of data, such as text, pictures, audio, and so on. The default location is:/data/data/< >/files/***.***.

You can create files for saving data in the device's own storage device or an external storage device. Similarly, in the default state, files are not shared among different programs.

Write file: Invokes the Context.openfileoutput () method to create a file based on the specified path and filename, which returns a FileOutputStream object.

Read file: Call the Context.openfileinput () method to return a standard Javafileinputstream object by setting the path and filename. The third type: SQLite store data

SQLite database databases. Android's support for the database is good, it integrates the SQLite database itself, each application is easy to use it, or rather, Android relies entirely on the SQLite database, all of its system data and the structured data it uses are stored in the database. SQLite is a lightweight embedded database engine that supports the SQL language and has good performance with very little memory. In addition it is open source and can be used by anyone. Many open source projects (Mozilla, PHP, Python) Use the sqlite.sqlite to consist of the following components: SQL compiler, Kernel, backend, and attachment. SQLite makes debugging, modifying, and extending the SQLite kernel easier by leveraging virtual machines and virtual database engines (VDBE).

SQLite it has the following advantages:

1. Superior efficiency

2. Very suitable for storage of structured data

3. Facilitates the transfer of data between different activity and even different applications. 4. Resource-limited equipment 5. No server process, 6. All data is stored in the same file across platforms, 7. Can be reproduced freely.
The database is stored under the data/< project folder >/databases/. The SQLite database is used in Android development activites can access a database through the Content Provider or Service. The Fourth kind: ContentProvider

An Android system that enables all applications to share a way of storing data, because the data is usually private among applications, so this storage is less used, but it is an essential storage method. For example, audio, video, pictures, and address books can be stored in this way in general. Each contentprovider provides a public URI (wrapped as a URI object), and if the application has data that needs to be shared, it needs to use ContentProvider to define a URI for the data, and then other applications pass the content The provider is passed in to the URI to manipulate the data.

  type Fifth: Networked storage The network is generally used to require real-time transmission of data, because the mobile generated traffic needs tariff, and now the Chinese mainland region's network speed is not satisfactory, so send data through the network need to pay attention to the amount of data, if not necessary, do not use the network to send. However, if some data can not be processed on the mobile side or handled more cumbersome, consider sending it over the network to the server for processing. Summary:

1. Simple data and configuration information, Sharedpreferences is preferred 2. If the amount of data is too large, sharedpreferences is not enough, you can consider using the database, processing the relationship is more complex. 3. For structured data, be sure to use the database, although it will appear to be troublesome, but the subsequent use of the benefit of the infinite 4. The ordinary file refers to the text file, binary file, multimedia file and so on 5. Try not to create files, use the API Provide the way to store files (sharedpreferences, database) 6. If you want to create a file, assuming that the file is a private file or an important file, you must keep it in the internal storage, and then store it in the external storage to 7. Avoid transferring large data volumes when using networked storage Should pay attention to the network transmission mode optimization (reduce the number of requests, etc.)





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.