1. About ContentProvider and Contentresolver
(1) ContentProvider (content provider)
ContentProvider is the standard API for data exchange between different applications, providing only the access interface to the data.
ContentProvider provides data externally in some form of URI, allowing other applications to access or modify the data, and other applications to specify data by contentresolver based on URI access operations.
Converts a string to a URI:
Uri uri = Uri.parse("content://com.gc.contentprovider/test/100");
The ContentProvider has only one onCreate () life-cycle method and is only called once, and the OnCreate () method is called back when the other app accesses the ContentProvider for the first time through Contentresolver.
(2) Contentresolver (content observer)
The content provides the following ways to get Contentresolver:
getContentResolver();
2.ContentProvider, Contentresolver, URI of the relationship between the three
3. Development ContentProvider
Developing ContentProvider takes two steps:
1th step: Inherit ContentProvider, implement query (), insert (), update (), delete () and other methods (these methods are called to other applications);
2nd Step: Register the ContentProvider in Androidmanifest.xml, specify the Android:authorities property (Specify the URI for the ContentProvider) and Android: The exported property (set Android:exported:true means allow other applications to call).
The next step is to actually develop a contentprovider:
(1) Create MyProvider, inherit ContentProvider
Public class MyProvider extends contentprovider { //Create call for first time @Override Public Boolean onCreate() {System.out.println ("OnCreate");return true; }The //return value indicates the MIME type of the data provided by the ContentProvider @Override PublicStringGetType(URI Uri) {System.out.println ("GetType");return NULL; }//Query method, return the cursor that the query gets @Override PublicCursorQuery(Uri Uri, string[] projection, string where, string[] Whereargs, string sortOrder) {System.out.println (URI +"-query"); System.out.println ("where parameter:"+ where);return NULL; }//Insert method to return the URI of the newly inserted record @Override PublicUriInsert(URI Uri, contentvalues values) {System.out.println (URI +"-insert"); System.out.println ("Values parameter:"+ values);return NULL; }//Delete method, returns the number of deleted record bars @Override Public int Delete(Uri Uri, String where, string[] Whereargs) {System.out.println (URI +"-delete"); System.out.println ("where parameter:"+ where);return 0; }//Delete method, returns the number of record bars that were updated @Override Public int Update(URI Uri, contentvalues values, String where, string[] Whereargs) {System.out.println (URI +"-update"); System.out.println ("where parameter:"+ where +", Values parameter:"+ values);return 0; }}
(2) Configuration ContentProvider
Add the following sub-elements under the application element in the Androidmanifest.xml
<!-- 注册一个ContentProvider --><!-- exported:指定是否允许其他应用调用,true允许 --><!-- name:指定ContentProvider实现类的类名 --><!-- authorities:指定ContentProvider对应的Uri --><provider android:exported="true" android:name=".MyProvider" android:authorities="com.gc.contentprovider.myprovider"></provider>
(3) Calling method using Contentresolver (in other applications)
Public class myresolver extends Activity {Contentresolver Resolver; Uri uri = Uri.parse ("content://com.gc.contentprovider.myprovider/");@Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Get the Contentresolver object of the systemResolver = Getcontentresolver (); } Public void Query(View Source) {//Call Contentresolver's query () method, which actually returns the return value of the ContentProvider query () corresponding to the URI .Cursor C = resolver.query (URI,NULL,"Query_where",NULL,NULL); System.out.println ("Remote ContentProvider returns the cursor:"+ c); } Public void Insert(View Source) {Contentvalues values =NewContentvalues (); Values.put ("Name","Android");//Call Contentresolver's Insert () method, which actually returns the return value of the insert () of the contentprovider corresponding to the URI .Uri Newuri = Resolver.insert (URI, values); System.out.println ("Remote ContentProvider the URI of the newly inserted record:"+ Newuri); } Public void Update(View Source) {Contentvalues values =NewContentvalues (); Values.put ("Name","Android");//Call Contentresolver's Update () method, which actually returns the return value of the update () of the contentprovider corresponding to the URI intCount = Resolver.update (URI, values,"Update_where",NULL); System.out.println ("Remote ContentProvider update Record count:"+ count); } Public void Delete(View Source) {//Call Contentresolver's Delete () method, which actually returns the return value of the ContentProvider Delete () corresponding to the URI . intCount = Resolver.delete (URI,"Delete_where",NULL); System.out.println ("Remote ContentProvider Delete record number:"+ count); }}
4. ContentProvider of the operating system
The steps to ContentProvider data using the Contentresolver operating system are still two steps:
1th Step: Call the Activity's Getcontentresolver () method to get the Contentresolver object;
2nd step: Call ContentProvider's query (), insert (), update (), delete () method to manipulate the data as needed.
Note: You need to know in advance the URI of the ContentProvider and the column names of the data columns that the ContentProvider manipulate, which can be obtained by reviewing the official documentation.
Android development Practices ContentProvider and Contentresolver