An example explains how to use the ContentProvider component in Android
ContentProvider is basically used. In order to exchange data between applications, Android provides ContentProvider. ContentProvider is a standard API for data exchange between different applications. When an application needs to expose its own data to other applications, the application can be realized by providing ContentProvider, Other applications can manipulate the data exposed by the ContentProvider through the contentresolver.
Steps to implement ContentProvider:
1) Write a class that inherits ContentProvider and overrides the crud method inside.
2) Register the provider in the Android manifest.xml file.
The following three attributes are required to register the provider in Android manifest.xml:
Android: implementation class of name provider.
Android: URI of the authorities provider.
Android: whether the exported provider is exposed to other programs.
Contentresovler operation ContentProvider:
1) Get the contentresolver. The getcontentresolver () method comes from the contextwrapper, so it can be used in both activity and service.
2) Call the curd method and call the method of the specified ContentProvider through the parameter URL.
The following is a demo, which inserts a piece of data into the ContentProvider and returns it to the listview.
main.xml:
Mysqliteopenhelper class
Mycontentprovider class
The display interface of listview is show.xml
Main.java
Implementation effect: (effect after 3 times of insertion)
Unit test of ContentProvider ContentProvider is one of the four components of Android. It is best to add unit test when writing code, so as to determine the correct crud of data. This article mainly introduces the use of two main auxiliary classes in ContentProvider and the use of unit tests in ContentProvider.
Two auxiliary classes are needed: urimatcher class and contenturis class.
Urimatcher class: it can match the input URI parameters on a row to determine what operations to perform on what table.
Contenturis class: some methods need to return URIs. Using this class can easily generate URI classes.
For unit testing, I think it is very necessary to use it when writing code in the future, so as to determine the correctness of the code very accurately.
To use unit tests:
1) Add instrumentation. The code of this part is fixed, and can also be completely imported in the wizard provided by ADT.
2) Add < uses Library >, and the code of this part is also written in a fixed way.
Well, the necessary knowledge has been finished. Now the code:
1) Generate a sqlitedatabase class, which is a required class mysqliteopenhelper class
Then add the mycontentprovider class we need:
This class is very long, but the execution methods are common curd methods. The important thing is the use of urimatcher and contenturis classes.
Then execute the unit test class: test
After the insert method is executed (three times):
After the update method is executed:
After executing the query method:
After the delete method is executed: