5 ways to store data in Android

Source: Internet
Author: User
Tags map data structure sqlite database

Introduction: This is the detailed page of the 5 method in data storage in Android, the knowledge of the 5 methods of data storage in the mobile software, Android Android, Android data storage
Android provides 5 ways of storing data:
--use sharedpreferences to store data;
--File storage data;
--sqlite database to store data;
--use ContentProvider to store data;
--Network storage data;

First of all, Preference,file, the database of the three different ways corresponding to the directory is/data/data/package name/shared_pref,/data/data/package name/files,/data /data/package Name/database. File storage is typically used in Android using Context.openfileoutput (string filename, int mode) and context.openfileinput (string filename).
The file generated by Context.openfileoutput (String fileName, int mode) is automatically stored in the/data/data/package name/files directory, and its full path is/data/data/ Package Name/files/filename. Note that the parameter filename here may not contain a path divider (such as "/").
In general, files generated in this way can only be accessed within this apk. But this conclusion refers to the use of the Context.openfileinput (String fileName) method. In this way, each APK can only access the files in its own/data/data/package Name/files directory, for the simple reason that the parameter filename is not included in the path separator, and Android automatically/data/data/ Look for files named filename in the package name/files directory.

One: Storing data using sharedpreferences
This first describes the Sharedpreferences storage method, which is a mechanism provided by Android to store some simple configuration information, such as the user name and password of the logged-in user. It uses the map data structure to store the information, stored as a key value, can be simply read and write, the concrete example is as follows:
void Readsharedpreferences () {
String Strname,strpassword;
Sharedpreferences user = Getsharedpreferences ("User_info", 0);
StrName = user.getstring ("NAME", "" ");
strpassword = User getString ("PASSWORD", "" ");
}
void Writesharedpreferences (String strname,string strpassword) {
Sharedpreferences user = Getsharedpreferences ("User_info", 0);
Uer.edit ();
User.putstring ("NAME", strName);
User.putstring ("PASSWORD", strpassword);
User.commit ();
}
The method of reading and writing data is very simple, but there are some differences when writing: Call edit () to edit the data before you can modify it, and then commit the modified data using commit (). In fact, sharedpreferences is using XML format to store the data in the device, under/data/data/<package name>/shares_prefs in the file Explorer in Ddms. As an example of the above data storage results, you can see a user_info.xml file when you open it, and you can see it when you open it:
<?xml version= "1.0″encoding=" utf-8″?>
<map>
<string name= "Name" >moandroid</string>
<string name= "PASSWORD" >SharedPreferences</string>
</map>
There are some limitations to using sharedpreferences: They can only be used within the same package and cannot be used between different packages.

Two: File storage data

File storage is a more common method, the method of reading/writing files in Android is exactly the same as the program implementing I/O in Java, providing the Openfileinput () and Openfileoutput () methods to read the files on the device. FilterInputStream, Filteroutputstream, etc. can go to the Java IO Package instructions to learn more detailed, no longer this detailed description, specific examples are as follows:
STRING fn = "Moandroid.log";
FileInputStream FIS = openfileinput (FN);
FileOutputStream fos = openfileoutput (fn,context.mode_private);
In addition to this, Android also provides other functions to manipulate files, detailed instructions please read the Android SDK.

Three: Networked storage data
Network storage, need to deal with Android network packets, about the Android network packet details, please read the Android SDK reference to the Java SDK which package?

Four: ContentProvider
1, ContentProvider Introduction
When an app inherits the ContentProvider class and overrides the method used to provide data and store data, it can share its data with other apps. Although the use of other methods can also share data, but the way data access will vary depending on how the data is stored, such as: the use of file-based data sharing, the need for file operations to read and write data, the use of sharedpreferences shared data, You need to read and write data using the Sharedpreferences API. The benefit of using ContentProvider to share data is to unify the way data is accessed.
2. Introduction to URI Class
The URI represents the data to manipulate, and the URI mainly contains two pieces of information: 1. ContentProvider to operate, 2. To manipulate what data is in ContentProvider, a URI consists of the following parts:
1.scheme:contentprovider (content Provider) scheme has been defined by Android as: content://.
2. hostname (or authority): used to uniquely identify this contentprovider, external callers can find it based on this identity.
3. Path: can be used to represent the data we want to manipulate, the path should be built according to the business, as follows:
? To manipulate records with ID 10 in the Contact table, you can build such a path:/CONTACT/10
? To manipulate the name field of the record with ID 10 in the Contact table, Contact/10/name
? To manipulate all records in the Contact table, you can build such a path:/contact?
The data you want to manipulate does not necessarily come from a database, or it can be stored as a file, as follows:
To manipulate the name node under the contact node in the XML file, you can build such a path:/contact/name
If you want to convert a string to a URI, you can use the parse () method in the Uri class, as follows:
Uri uri = uri.parse ("Content://com.changcheng.provider.contactprovider/contact")
3, Urimatcher, Contenturist and Contentresolver Introduction
Because the URI represents the data to manipulate, we often need to parse the URI and fetch the data from the URI. The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively. Mastering their use will facilitate our development efforts.
? Urimatcher: Used to match the URI, it uses the following:
1. First put the URI path you need to match all to the registration, as follows:
The constant Urimatcher.no_match represents a return code (-1) that does not match any path.
Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);
If the match () method matches the Content://com.changcheng.sqlite.provider.contactprovider/contact path, a match code of 1 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "Contact", 1);//Add need to match URI, if match will return match code
If the match () method matches the content://com.changcheng.sqlite.provider.contactprovider/contact/230 path, a match code of 2 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "contact/#", 2);//#号为通配符

2. After registering a URI that needs to be matched, you can use the Urimatcher.match (URI) method to match the input URI, and if the match returns a matching code, the match code is the third parameter passed in by calling the Adduri () method, assuming that the match content:// Com.changcheng.sqlite.provider.contactprovider/contact path, the matching code returned is 1.
?
Contenturis: Used to get the ID part after the URI path, it has two more practical methods:
? Withappendedid (URI, id) is used to add the ID portion of the path
? The Parseid (URI) method is used to get the ID part from the path
? Contentresolver: When an external application needs to add, delete, modify, and query the data in ContentProvider, you can use the Contentresolver class to get the Contentresolver object, You can use the Getcontentresolver () method provided by the activity. Contentresolver uses the INSERT, delete, update, and query methods to manipulate the data.

V: Summary Notes
The above 5 storage methods, in the future development process, according to design objectives, performance requirements, space requirements, and so find the appropriate data storage methods. Data stores in Android are private, and other applications are inaccessible unless the data shared by other programs is obtained through Contentresolver. The use of file-sharing data, the need for file operations to read and write data, the use of sharedpreferences shared data, need to use the Sharedpreferences API to read and write data. The benefit of using ContentProvider to share data is to unify the way data is accessed.

5 ways to store data in Android

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.