Contentprovider Introduction
Let's talk about Android applications.ProgramThe four core components are activity, service, broadcastreceiver, and contentprovider. In Android, applications are independent of each other, and they all run in their own virtual machines. Contentprovider provides a way to share data between programs. A program can use contentprovider to define a URI and provide a unified operation interface. Other programs can use this URI to access specified data, add, delete, modify, and query data.
Here is a demo of contentprovider's access to the contact information,
First, create a contectsdemo Android project:
Next, let's take a look at 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: Orientation = "vertical"> <textview Android: id = "@ + ID/text" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <button Android: id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "get contact information"/> </linearlayout>
Then let's take a look at the main program:
Public class contectsdemoactivity extends activity {/** called when the activity is first created. */private button button1; private textview text; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); text = (textview) This. findviewbyid (R. id. text); button1 = (button) This. findviewbyid (R. id. button1); button1.setonclicklistener (New View. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub stringbuilder sb = getcontacts (); text. settext (sb. tostring () ;}}) ;}private stringbuilder getcontacts () {stringbuilder sblog = new stringbuilder (); // obtain the contentresolver object contentresolver Cr = This. getcontentresolver (); // get the cursor of the first item in the phone book, mainly querying the "contacts" Table cursor = CR. query (contactscontract. contacts. conten T_uri, null, null); If (! Cursor. movetofirst () {sblog. append ("the retrieved content is empty! "); Return sblog;} If (cursor. movetofirst () {// obtain the contact name (displayed name). The actual content is in contactscontract. int nameindex = cursor in contacts. getcolumnindex (contactscontract. contacts. display_name); string name = cursor. getstring (nameindex); sblog. append ("name =" + name + ";"); // obtain the contact ID string contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. _ id); // query the corresponding phone number cursor phonenumbers = Cr Based on the contact ID. query (contactscontract. commondatakinds. phone. content_uri, null, contactscontract. commondatakinds. phone. contact_id + "=" + contactid, null, null); // obtain the phone number (multiple numbers may exist) while (phonenumbers. movetonext () {string strphonenumber = phonenumbers. getstring (phonenumbers. getcolumnindex (contactscontract. commondatakinds. phone. number); sblog. append ("phone =" + strphonenumber + ";");} phonenumbers. close (); // query the email cursor emails = Cr Based on the contact ID. query (contactscontract. commondatakinds. email. content_uri, null, contactscontract. commondatakinds. email. contact_id + "=" + contactid, null, null); // obtain an email (multiple emails may exist) while (emails. movetonext () {string stremail = emails. getstring (emails. getcolumnindex (contactscontract. commondatakinds. email. data); sblog. append ("email =" + stremail + ";");} emails. close ();} cursor. close (); log. E ("-------------------", sblog. tostring (); Return sblog ;}}
Do not forget to add the access permission:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/> <uses-Permission Android: Name = "android. Permission. write_contacts"/>
Finally, let's take a look at the effect:
Click the get contact button to see what the effect is:
In this way, we get the contact name, phone number, and email. To prove this, let's take a look at the address book information in my simulator:
OK! Cursor. movetonext () can be used to obtain multiple items. Here I will not add any additional items. Since you can read the contact information, you can add the contact information through contentprovider and repeat it later.