The best way to understand ContentProvider is to try it yourself. The following describes how to use a built-in contacts contentprovider.
1. Create a project, Provider.
2. Code in the Main.xml.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<listview
android:id= "@+id/android:list" android:layout_width= "Fill_"
Parent "
android:layout_height=" wrap_content "
android:layout_weight=" 1 "
android:stackfrombottom=" False "
android:transcriptmode=" normal "/>
<textview
android:id=" @+id/contactname "
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:textstyle= "bold"/ >
<textview
android:id= "@+id/contactid"
android:layout_width= "Fill_parent"
android: layout_height= "Wrap_content"/>
</LinearLayout>
3. Code in the Provideractivity.java.
The public class Provideractivity extends Listactivity {/** called ' when the ' activity is ' is a. */@Overr
IDE public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Uri allcontacts = Uri.parse ("Content://contacts/people");
Uri allcontacts = ContactsContract.Contacts.CONTENT_URI;
Cursor C; if (Android.os.Build.VERSION.SDK_INT < one) {//before honeycomb C = managedquery (Allcontac
TS, NULL, NULL, NULL, NULL); else {//Honeycomb and later cursorloader cursorloader = new Cursorloader (This, allcontacts
, NULL, NULL, NULL, NULL);
c = Cursorloader.loadinbackground ();
} string[] columns = new string[] {ContactsContract.Contacts.DISPLAY_NAME, contactscontract.contacts._id };
Int[] views = new int[] {r.id.contactname, r.id.contactid};
Simplecursoradapter adapter; if (Android.os.Build.VERSION.SDK_INT < one) {//before honeycomb adapter = new Simplecursor
Adapter (This, R.layout.main, C, columns, views); else {//honeycomb and later adapter = new Simplecursoradapter (this, R.layout.main, C, Colu
MNS, views, cursoradapter.flag_register_content_observer);
} this.setlistadapter (adapter); }
}