Detailed explanation of ContentProvider instance in Android

Detailed explanation of ContentProvider instance in Android

Brief introduction to content provider:

*The content provider mechanism in Android can support storing and reading data in multiple applications. This is also cross application

The only way to share data. In the Android system, there is no common memory area for multiple applications to share and store data;

*Android provides content providers of some main data types, such as audio, video, pictures and private address books;

Find some ContentProviders provided by Android under the android.provider package; You can query the data they contain by obtaining the ContentProvider (the query condition is that you have obtained the appropriate read permission);

1. Introduction to URI:

Resolution:

1.1: the scheme of the ContentProvider has been specified by Android. The scheme is content: / / host name (or authority), which is used to uniquely identify the ContentProvider. External callers can find it according to this identification. The path path can be used to represent the data we want to operate. The construction of the path should be determined according to the business, for example:

(1) To operate the record with ID 10 in the person table, the build path is as follows: / person / 10; (2) To operate the name field of the record with ID 10 in the person table: person / 10 / name; (3) To operate all records in the person table: / person; (4) To operate the records in the XXX table: / xxx; (5) If the data part to be operated is from the database, but from other storage methods such as file, XML or network;

For example, to operate the name node under the person node in the XML file, the build path is as follows: / person / name

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

2. Introduction to the use of urimatcher class:

Since URI represents the data to be operated, it is often necessary to parse URI and obtain data from URI. Android provides two tool classes for operating URI:

* UriMatcher * ContentUris

Urimatcher class is used to match URI. Its usage is as follows:

Tips:

After registering the URI that needs to be matched, you can use the smatcher. Match (URI) method to match the input URI. If it matches

It returns the matching code, which is the third parameter passed in by calling the adduri () method;

3. Introduction to the use of contenturis class:

Contenturis class is used to obtain the ID part behind the URI path. There are two practical methods:

If you have any questions, please leave a message or go to the community of this website for communication and discussion. Everyone can make progress together. Thank you for reading. I hope it can help you. Thank you for your support for benzene,

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