Detailed explanation of the use of ContentProvider developed by Android

The content provider provides a unified interface for storing and obtaining data. It can complete data sharing under different applications. SQLite mentioned in the usage method of SQLite developed by Android in the previous article can only share data in the same program. In addition, Android provides a content provider for some common data, such as audio, video, pictures, contacts, etc., so that we can easily operate these types of data. The advantage of using ContentProvider is that developers do not need to consider how data is stored internally. For example, if we want to use ContentProvider to store data, we just need to tell the insert function the URI of the ContentProvider and the data to be stored (including column names and values). The same is true during query. We only need to enter URI and query table, column names and query conditions, We don't need to know how these operations are performed in the ContentProvider.

Experimental basis

To understand the content of this experiment, you need to use the following classes related to 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:

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

Experimental process

This article uses an example to implement a ContentProvider. In fact, generally, we do not need to implement it ourselves, but use the built-in ContentProvider of andorid. However, after we implement it ourselves, we can have a deeper understanding of its principle, and it will be easy to use the built-in in later. This is what Mr. Mars said. I don't have enough heat. I haven't had a deep experience yet. Mr. Mars summarized the steps of implementing ContentProvider as follows:

Four java files are required in the program. Here are the precautions for implementing these classes:

Firstprovidermetadata class:

Because many constants are used in the subclasses obtained from the inherited class firstcontentprovider, we have created a new class to store these constants. This class is called firstprovidermetadata, which contains the author name, database name, database version number, table name, word table name, sub table URI, sub table ContentProvider data type, etc. The word list inherits the basecolumns class, which already has_ ID and_ Count these two columns.

Databasehelper class:

Similar to using SQLite in Android, we also need a subclass that inherits sqliteopenhelper. The subclass name is databasehelper. We create a table in the callback function oncreate() of the subclass. The table name and column name in the table refer to the constants defined in the firstprovidermetadata class.

Firstcontentprovider class:

Create a new class named firstcontentprovider, which inherits the class ContentProvider, and must override the following 5 methods of the parent class, otherwise an error will be reported. The five methods are oncreate(), gettype(), insert(), update(), delete()

Oncreate() is a callback function, which is called when ContentProvider is created. This program uses databasehelper to create a SQLite database in this function.

The function completed in the GetType () method is to return the data type represented by the URI according to the incoming URI. Inside the function, the urimatcher is used to match the parameters passed in by the function to get its data type.

The insert () function inserts a specified value into the specified database table. After the insertion is completed, a new record will be generated, and then a new URI will be generated by using the record and table URI. This URI represents the URI of the record successfully inserted, and this function also returns this URI.

Mainactivity class:

In mainactivity, there are mainly two buttons, which are bound with listeners to complete insertion and query operations.

When you click the insert button, you will first get a contentresolver in the listener function, and then automatically call the insert method of the ContentProvider when you execute the insert method of the contentresolver, because the first parameter in the insert method of the contentresolver is the URI of a ContentProvider.

  AndroidManifest.xml:

The use of ContentProvider needs to be registered in androidmanifest.xml, and the following declaration can be added outside the activity tag:

Codes and notes of main parts of the experiment:

MainActivity.java:

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
分享
二维码
< <上一篇
下一篇>>