Basic principle and usage of Android ContentProvider

Other applications can access the data provided by the ContentProvider through the contentresolver, and the contentresolver locates the data to be accessed through the URI, so we need to understand the URI first. Uri (Universal Resource Identifier) uniform resource locator. If you have used Android implicit startup, you will find that in the process of implicit startup, we also use the URI to locate the activity we need to open, and parameters can be passed in the URI.

Uri gives a name to each resource in the system, such as call records. Each ContentProvider has a public URI to represent the data provided by the ContentProvider. The URI format is as follows:

The third part of the path is further explained to represent the data to be operated. It should be built according to the actual project requirements. For example:

Let's take another example:

Uri represents the data to be manipulated. It needs to be parsed when obtaining data during development. Android provides two tool classes for manipulating URI, urimatcher and contenturis. Mastering their basic concepts and usage is a necessary skill for an Android Developer.

The urimatcher class is used to match URIs. Its usage steps are as follows:

Here, two required URIs are registered with adduri; Note that when adding the second URI, the ID after the path adopts the wildcard form "#", which means that it is OK as long as the first three parts are matched.

Contenturis class is used to operate the ID part behind the URI path. It has two practical methods: withappendedd (URI URI, long ID) and parseid (URI URI URI).

Dbhelper.java (operation database)

Myprovider.java (custom ContentProvider)

Mainactivity.java (contentresolver operation)

Finally, the manifest states:

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