Detailed explanation of ContentProvider and URI in Android learning notes

This article introduces the related contents of the custom content provider and completely resolves the usage of the content provider. Content provider, content provider, I believe everyone is familiar with the name of this component. It may be that they usually do some simple apps, so they are not very familiar with the use of content provider. However, here is a simple summary of the content provider, which is not very in-depth, but I hope to give some help to beginners including me. After reading this article, I can have an overall understanding of this component.

1、 Use the ContentProvider to share data

The function of ContentProvider in Android is to share data externally, that is, you can share the data in your application with other applications through ContentProvider, and other applications can add, delete, modify and query the data in your application through ContentProvider. As for data sharing, we have learned the file operation mode before and know that by specifying the file operation mode as context MODE_ WORLD_ Readable or context MODE_ WORLD_ Writeable can also share data externally. So why use ContentProvider to share data? Yes, if the file operation mode is used to share data externally, the data access methods will vary according to the data storage mode, resulting in the data access methods can not be unified. For example, if the XML file is used to share data externally, XML parsing is required to read the data; To share data with shared preferences, you need to use the shared preferences API to read the data. The advantage of using ContentProvider to share data externally is that it unifies the data access mode. When the application needs to share data externally through the ContentProvider, the first step is to inherit the ContentProvider and rewrite the following method:

The second step needs to be in androidmanifest XML uses < provider > to configure the ContentProvider. In order to enable other applications to find the ContentProvider, the ContentProvider uses authorities (host name / domain name) to uniquely identify it. You can think of the ContentProvider as a website (think, the website is also the data provider). Authorities is its domain name:

2、 Uri introduction URI represents the data to be operated. URI mainly contains two parts of information: 1) the ContentProvider to be operated and 2) what data in the ContentProvider to operate. A URI consists of the following parts:

The scheme of the ContentProvider has been specified by Android. The scheme is: content://

The host name (or authority) is used to uniquely identify the ContentProvider, which can be found by external callers.

The path can be used to represent the data we want to operate. The construction of the path should be determined according to the business, as follows:

To manipulate the record with ID 10 in the person table, you can build such a path: / person / 10

To manipulate the name field of the record with ID 10 in the person table, person / 10 / name

To manipulate all records in the person table, you can build such a path: / person

To manipulate the records in the XXX table, you can build such a path: / xxx

Of course, the data to be operated does not necessarily come from the database, but can also be stored in other ways such as file, XML or network, as follows:

To operate the name node under the person node in the XML file, you can build such a path: / person / name

If you want to convert a string to URI, you can use the parse () method in the URI class, as follows:

3、 Introduction to the use of urimatcher class

Because URI represents the data to be manipulated, we often need to parse URI and get data from URI. The Android system provides two tool classes for operating URI, urimatcher and contenturis. Mastering their use will facilitate our development work. Urimatcher class is used to match URI. Its usage is as follows:

The first step is to register all the URI paths you need to match, as follows:

After registering the URI that needs to be matched, you can use smatcher The match (URI) method matches the input URI. If it matches, it returns the matching code. The matching code is the third parameter passed in by calling the adduri () method. It is assumed that it matches content://com.ljq.provider.personprovider/person Path, the returned matching code is 1

4、 Introduction to the use of contenturis class

Contenturis class is used to operate the ID part behind the URI path. It has two practical methods:

Withappendedd (URI, ID) is used to add the ID part to the path:

The parseid (URI) method is used to get the ID part from the path:

5、 Sharing data using ContentProvider

The main functions of ContentProvider class methods are as follows:

Public Boolean oncreate() this method will be called after the ContentProvider is created. After Android is powered on, the ContentProvider will be created when other applications access it for the first time.

Public URI insert (URI URI, contentvalues) this method is used by external applications to add data to the ContentProvider.

Public int delete (URI, URI, string [] selectionargs) this method is used by external applications to delete data from the ContentProvider.

Public int update (URI, URI, string [] selectionargs) this method is used by external applications to update the data in ContentProvider.

Public cursor query (URI, URI, string sortorder) this method is used by external applications to obtain data from ContentProvider.

Public string GetType (URI): this method is used to return the MIME type of the data represented by the current URL. If the data of the operation belongs to the collection type, the MIME type string should start with vnd android. cursor. Dir /,

For example: to get all person records, the URI is content://com.ljq.provider.personprovider/person , the returned MIME type string should be: "vnd. Android. Cursor. Dir / person".

If the data to be manipulated is non collection type data, the MIME type string should start with vnd android. cursor. Start with item /,

For example, get the person record with ID 10 and URI content://com.ljq.provider.personprovider/person/10 , the returned MIME type string is "vnd. Android. Cursor. Item / person".

6、 Use contentresolver to manipulate data in ContentProvider

When an external application needs to add, delete, modify and query the data in the ContentProvider, it can use the contentresolver class. To obtain the contentresolver object, you can use the getcontentresolver () method provided by the activity. The contentresolver class provides four methods with the same signature as the ContentProvider class:

Public URI insert (URI URI, contentvalues) this method is used to add data to the ContentProvider.

Public int delete (URI, URI, string [] selectionargs) this method is used to delete data from the ContentProvider.

Public int update (URI, URI, string [] selectionargs) this method is used to update the data in the ContentProvider.

Public cursor query (URI, URI, string sortorder) this method is used to obtain data from ContentProvider.

The first parameter of these methods is URI, which represents the ContentProvider to be operated and what data to operate on. It is assumed that the given is URI parse(" content://com.ljq.providers.personprovider/person/10 "), then the host name will be com ljq. providers. The ContentProvider of the person provider operates on the record with ID 10 in the person table. Use contentresolver to add, delete, modify and query data in ContentProvider:

7、 Listen for data changes in ContentProvider

If the visitor to the ContentProvider needs to know that the data in the ContentProvider changes, you can call getcontentresolver () when the data in the ContentProvider changes Notifychange (URI, null) to notify visitors registered on this URI, for example:

If the visitor to the ContentProvider needs to get the data change notification, he must use contentobserver to listen to the data (the data is described by URI). When he hears the data change notification, the system will call the onchange() method of contentobserver:

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