Four ways to store Android data
As a finished application, data storage operations are essential. As a result, the Android system offers four ways to store data. are: Sharepreference, SQLite, Content provider, and file. As the Android system, the data are basically private, are stored in the "data/data/package name" directory, so to achieve data sharing, the correct way is to use the content Provider.
SQLite: SQLite is a lightweight database that supports basic SQL syntax and is often used as a way of storing data. Android provides a class named Sqlitedatabase for this database, encapsulating some of the APIs that manipulate the database.
sharedpreference: In addition to the SQLite database, another common method of data storage, which is essentially an XML file, is often used to store simpler parameter settings.
File : the commonly used method of storing files (I/O), often storing large amounts of data, but the drawback is that updating the data will be a difficult task.
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 content provider provides a public URI (wrapped as a URI object), and if the application has data to share, it needs to use content provider to define a URI for the data, and then the other application passes the content Provider pass in the URI to manipulate the data.
second, the use of sharedpreference:
Sharedpreferences is also a lightweight method of data storage, which is essentially based on XML files that store Key-value key-value pairs of data, typically used to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. The Sharedpreferences object itself can only fetch data without support for storage and modification, and is implemented through the editor object (re-assigning a key to the data for modification). The storage steps for implementing Sharedpreferences are as follows:
1. Get Sharedpreferences objects based on context
2. Use the edit () method to get the editor object
3. Storing Key-value key value pair data via Editior object
4. Submit data through the commit () method
The code is as follows:
Public classPreferenceutil {Private StaticString preference = "Testfile_file"; Public Staticstring Getstringpreference (context context, string key, String defaultvalue) {Sharedpreferences preferences /c7>=context.getsharedpreferences (preference, context. Mode_private); String Test=preferences.getstring (key, DefaultValue); returntest; } Public Static voidsetstringpreference (context context, string key, String value) {Sharedpreferences preferences=context.getsharedpreferences (preference, context. Mode_private); Sharedpreferences.editor Editor=Preferences.edit (); Editor.putstring (key, value); Editor.commit (); }}
4 ways to store Android data