Introductory article: 9. Component 4:contentprovider (content provider)

Source: Internet
Author: User

Android four basic components of contentprovider (content provider) This component is actually an application for data or database to provide, share other applications to obtain data components, the approximate structure of the diagram is as follows:

Where the URI is an important intermediary medium.

Contact the Android app that we usually use, the most common is the Address book. In our Android phone, the design of the address book is actually two programs, one of which has no user interface, simple storage of contact information, and database structure is complex, this is to ensure the security of data. Another program is the Address Book program that we can see. Then the first program is a contentprovider, to provide data, and contacts this Android phone built-in software, is the ContentProvider provided by the program to increase the deletion and check.

If we want to write a contentprovider ourselves, we need to customize the class to inherit the ContentProvider, and implement the method in which we customize this contentprovider to provide data to other applications or programs. The specific structure of this class and the method code in it are as follows, and the meanings of each method are visible in the comments:

ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;Importandroid.support.annotation.Nullable;/*** Created by Lzc on 16/6/22.*/ Public classMycontentproviderextendsContentProvider {@Override Public BooleanOnCreate () {//called when the ContentProvider is created        return false; } @Nullable @Override PublicCursor query (Uri URI, string[] strings, string s, string[] strings1, string s1) {//all records that match the criteria set by the selection are queried based on the URI, and you can specify how those columns are sorted by (order)        return NULL; } @Nullable @Override PublicString GetType (Uri uri) {//returns the MIME type of the current URI, if the data corresponding to the URI may include multiple records//then the MIME type string is the beginning of vnd.android.dir///if the data for the URI has only one record, the MIME type string begins with vnd.android.crusor.item/        return NULL; } @Nullable @Override PublicURI insert (Uri Uri, contentvalues contentvalues) {//Insert values corresponding to a URI        return NULL; } @Override Public intDelete (Uri Uri, String s, string[] strings) {//all records matching the criteria specified by the selection are deleted according to the URI        return0; } @Override Public intUpdate (URI Uri, Contentvalues contentvalues, String S, string[] strings) {//all records matching the criteria specified by the selection are modified according to the URI        return0; }}

This contentprovider in practical applications we do not often go to their own definition, more often we just use the system, or other applications of the ContentProvider.

Here is a simple application that allows you to view the Address book information in your phone.

The code is as follows:

ImportAndroid.content.ContentResolver;ImportAndroid.content.CursorLoader;ImportAndroid.database.Cursor;Importandroid.provider.ContactsContract;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Contentresolver CR=Getcontentresolver (); Cursor C= Cr.query (ContactsContract.Contacts.CONTENT_URI,Newstring[]{contactscontract.contacts._id, ContactsContract.Contacts.DISPLAY_NAME},NULL,NULL,NULL); if(c!=NULL){             while(C.movetonext ()) {intid = c.getint (c.getcolumnindex ("_id")); System.out.println ("Id=" +ID); String name= C.getstring (C.getcolumnindex ("Display_name")); System.out.println ("Name:" +name); Cursor C1= Cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NewString[]{contactscontract.commondatakinds.phone.number, ContactsContract.CommonDataKinds.Phone.TYPE}, contactscontract.commondatakinds.phone.contact_id+ "=" +id,NULL,NULL); if(c1!=NULL){                         while(C1.movetonext ()) {intType =C1.getint (C1.getcolumnindex (ContactsContract.CommonDataKinds.Phone.TYPE)); if(Type = =ContactsContract.CommonDataKinds.Phone.TYPE_HOME) LOG.I ("Info", "Home phone:" +c1.getstring (C1.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER))); Else{log.i ("Info", "Mobile:" +c1.getstring (C1.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)));            }}} c1.close ();    }} c.close (); }}

Note: You need a user right here:

<android:name= "Android.permission.READ_CONTACTS"/>

Write a simple app feature to insert a message into your phone's address book.

The code is as follows:

ImportAndroid.content.ContentResolver;ImportAndroid.content.ContentUris;Importandroid.content.ContentValues;ImportAndroid.net.Uri;Importandroid.provider.ContactsContract;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Contentresolver CR=Getcontentresolver (); //insert a row of dataContentvalues Valuse =Newcontentvalues (); Uri URI=Cr.insert (Contactscontract.rawcontacts.content_uri,valuse); Long raw_contact_id=Contenturis.parseid (URI);        Valuse.clear (); //Insert Person NameValuse.put (contactscontract.commondatakinds.structuredname.raw_contact_id,raw_contact_id); Valuse.put (ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,"Android Entry Step"); Valuse.put (ContactsContract.CommonDataKinds.StructuredName.MIMETYPE,        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); URI=Cr.insert (Contactscontract.data.content_uri,valuse); //Inserting phone informationvaluse.clear ();        Valuse.put (contactscontract.commondatakinds.phone.raw_contact_id,raw_contact_id); Valuse.put (ContactsContract.CommonDataKinds.Phone.NUMBER,"131111111111"); Valuse.put (ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM        _type); URI=Cr.insert (Contactscontract.data.content_uri,valuse); }}

Note: The required user rights are:

    <android:name= "Android.permission.READ_CONTACTS"/>    < Android:name = "Android.permission.WRITE_CONTACTS"/> 

General system-provided ContentProvider include:

-Find Contacts

-Add Contacts

-SMS Reading

-Call History

-Multimedia picture Video audio

So far, the four basic components of Android development, the finale!

Introductory article: 9. Component 4:contentprovider (content provider)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.