Android foundation summary part 8: create and call your own ContentProvider

Today, let's explain how to create and call our own ContentProvider.

In the previous two articles, we talked about how to read and write contacts and short messages respectively. I believe you have a certain understanding of the operation method of ContentProvider. On some occasions, in addition to operating the ContentProvider, we may need to create our own ContentProvider to provide information sharing services, which requires us to master the creation and use skills of ContentProvider. Let's explain each step step step by step from the outside to the inside.

Before formally starting the example demonstration, let's understand the following two knowledge points:

to grant authorization:

In Android, each ContentProvider will register itself with a string similar to the domain name, and we become the authority. This uniquely identified string is the basis of a set of URIs that can be provided by this ContentProvider. Only with this basis can we provide information sharing services to the outside world.

Authorization is completed in androidmanifest.xml. Each ContentProvider must declare and authorize here, as follows:

The < provider > element above indicates that the provider of ContentProvider is the "someprovider" class and authorizes it. The basic URI of authorization is "com.your-company.someprovider". With this authorization information, the system can accurately locate the specific ContentProvider, so that visitors can obtain the specified information. This is very similar to the way you browse web pages. "Someprovider" is like a specific server, and "com.your-company.someprovider" is like a registered domain name. I believe you are not unfamiliar with this concept. Therefore, you can understand the role of ContentProvider authorization by associating it. (it should be noted that in addition to Android built-in applications, third-party programs should try to use the fully qualified authorization name in the above way.)

MIME type:

Just as the website returns the mime (Multipurpose Internet mail extensions) type of the given URL (which enables the browser to view the content with the correct program), the ContentProvider is also responsible for returning the MIME type of the given URI. According to the MIME type specification, a MIME type consists of two parts: type and subtype. For example: text / HTML, text / CSS, text / XML, etc.

Android follows a similar convention to define MIME types.

For a single record, the MIME type is similar to:

vnd. android. cursor. item/vnd. your-company. content-type

For a collection of records, the MIME type is similar to:

vnd.android.cursor.dir/vnd.your-company.comtent-type

Vnd indicates that these types and subtypes have non-standard, supplier specific forms; The content type can be determined according to the functions of the ContentProvider. For example, the ContentProvider of the journal can be note, the ContentProvider of the schedule can be schedule, and so on.

After understanding the above two knowledge points, we will demonstrate the specific process with examples.

We will create a ContentProvider that records person information to implement crud operation on person. Visitors can operate our ContentProvider through the following path:

Visitors can operate the person collection through "[base_uri] / persons", or operate a single person in the form of "[base_uri] / persons / #". We need two steps to create a person's ContentProvider:

1. Create the personprovider class:

We need to inherit the ContentProvider class and implement the oncreate, query, insert, update, delete and GetType methods. The specific codes are as follows:

In the personprovider, we have defined the authorization address as "com.scott.provider.personprovider". I believe you have also learned about it earlier. Based on this authorization, we use a urimatcher to match the paths, "[base_uri] / persons" and "[base_uri] / persons / #" These two paths are also described above and correspond to the operations of a record set and a single record respectively. In the query, insert, update and delete methods, we judge whether the URI is an operation record set or a single record according to the matching results of the urimatcher, so as to adopt different processing methods. In the GetType method, we will return different MIME types according to the matching results, This step cannot be missed. For example, in the query method, we may query all collections or a single record, and the cursor or collection type or single record we return is consistent with the MIME type returned by GetType. Just like browsing a web page, the browser should receive the type of information returned by the specified URL In addition, we note that in the above code, the notifydatachanged method is called in the insert, update and delete methods. The only step in this method is to notify the visitors of "[base_uri] / persons" that the data has changed and should be reloaded.

In our personprovider, we use the person and dbhelper classes. The code is as follows:

Finally, to make this ContentProvider effective, we need to declare and authorize it in androidmanifest.xml, as shown below:

Among them, Android: multiprocess represents whether multiprocess operation is allowed. In addition, we can also declare corresponding permissions for it. The corresponding attribute is Android: permission.

2. Call the personprovider class:

After completing the person's ContentProvider, let's take a look at how to access it. This step is completed in mainactivity. See the following code:

We can see that in the above code, it is relatively simple to test each case respectively. We will mainly talk about registercontentobserver.

In the previous personprovider, we also mentioned that after the data is changed, a notification will be sent to the specified URI visitor to update the query record. Please note that the contribution of the ContentProvider is not enough. We also need to register a contentobserver in the visitor to receive this notification. Let's create a

In this way, when the ContentProvider sends a notification, we can immediately receive it, so as to send a message to the handler to re query the records, so that we can see the latest record information.

Finally, we'll be at Android manifest Add MIME type filter for mainactivity in XML to tell the system the information types that mainactivity can process:

This completes the visitor's code. Let's see the effect:

Since there are too many operation types, I won't show them here. You can try it yourself.

Original link: http://blog.csdn.net/liuhe688/article/details/7050868

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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