Detailed explanation of ContentProvider mechanism for Android data persistence

This paper illustrates the ContentProvider mechanism of Android data persistence. Share with you for your reference, as follows:

Generally speaking, the data established by the applications of the Android operating system is only allowed to be used by themselves, and the applications cannot share data with each other with the help of public memory. The Android system provides a mechanism, namely content provider, to disclose their private data to the data content. Through this mechanism, It can be used by other applications to read their own internal data. Of course, it can also access the data of other applications. Generally, the content provider is supported by SQLite database to store internal data.

Differences between implementing ContentProvider and SQLite:

When the application program implements SQLite, the application program directly interfaces with the database, so it is necessary to implement SQLite interfaces in the application program: dB. Oncreate(), DB. Insert(), DB. Update(), DB. Delete(), DB. Query() and DB. Close();

When implementing the content provider, a ContentProvider program should be implemented between the application and the database. The ContentProvider program will directly connect with the database. At this time, the application needs to implement the method of interfacing with the ContentProvider program.

Here's how to create a content provider:

First, implement the content provider interface:

Five important methods to realize the interface;

Secondly, define the data URI:

The provider application needs to define a "base" URI for other applications to access the content provider. This URI must be unique and start with "content: / /". Content: indicates the location of data controlled by the content provider; At androidmanifest xml@R_ 419_ 1970 @ add the following code for declaration:

Add the following code to the application:

Define the location of a URI and set a variable to find the interface of the content provider program;

The following is a complete code, which is used to establish a content provider and add and retrieve data through the content provider program:

Code implementing the content provider interface:

To configure the androidmanifest.xml file:

A SQLite database system is established to store and manage data. At the same time, sqliteopenhelper class is used to assist in the establishment of database and sqlitedatabase class to manage database:

Here is the provider application:

The provider application can retrieve the database and add data to it through the content provider.

Several important classes in ContentProvider:

UriMatcher:

To understand urimatcher, you first need to understand the URI representation method in Android. As we all know, URI is a general resource identifier, which represents the data to be operated. Every resource in Android (such as text, image, video, etc.) can be represented by URI. The URI in Android consists of the following three parts: "content: / /" (i.e. author), data path and resource ID (optional). If there is an ID, it represents a specific resource; if there is no ID, it represents the whole under the path. Therefore, the three parameters of the adduri () function also correspond to the above three parameters. The matching process of urimatcher is divided into three steps: initialize urimatcher; Uri required for registration; Match the registered URI.

ContentResolver :

When using ContentProvider to share data in different applications, its data exposure method is similar to that of tables in the database. The contentresolver uses a database like method to access data from the ContentProvider. It queries the data provided in the ContentProvider through URI. When querying, it also needs to know the name of the destination database, the data type of the data segment, or the ID of the resource.

sqliteQueryBuilder:

It is an auxiliary class used to produce SQL query statements, which can easily access sqlitedatabase. When constructing SQL query statements, it also needs to specify table names, column names, where conditions, etc.

For more Android related content, readers who are interested can view the topics on this site: summary of Android resource operation skills, summary of SD card operation methods for Android programming development, summary of Android file operation skills, summary of Android database operation skills, summary of activity operation skills for Android programming, introduction and advanced tutorial for Android development Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>