Package cn. test; import java. util. arrayList; import android. content. contentProviderOperation; import android. content. contentResolver; import android. content. contentUris; import android. content. contentValues; import android. database. cursor; import android.net. uri; import android. provider. contactsContract; import android. provider. contactsContract. commonDataKinds. email; import android. provider. contactsContract. commonDataKinds. phone; import android. provider. contactsContract. commonDataKinds. structuredName; import android. test. androidTestCase; import android. util. log; public class TestContact extends AndroidTestCase {private static final String TAG = "TESTCONTACT"; private ContentResolver contentResolver; // access all contacts in the address book public void testContacts () {contentResolver = this. getContext (). getContentResolver (); Uri allContactsUri = Uri. parse ("content: // com. android. contacts/contacts "); Cursor allContactsCursor = contentResolver. query (allContactsUri, null, null); // obtain all contacts from the contacts table StringBuilder builder = new StringBuilder (); while (allContactsCursor. moveToNext () {int _ id = allContactsCursor. getInt (allContactsCursor. getColumnIndex ("_ id"); builder. append ("_ id =" + _ id ). append (","); Uri perContactUri = Uri. parse ("content: // com. android. contacts/"+ _ id +"/data "); Cursor perContactsCursor = contentResolver. query (perContactUri, null, null); // obtain all data of an id in the data table while (perContactsCursor. moveToNext () {// the following two sentences involve associating the data table and mimetype table for String data1 = perContactsCursor. getString (perContactsCursor. getColumnIndex ("data1"); // obtain the data1String mimetype = perContactsCursor of each contact. getString (perContactsCursor. getColumnIndex ("mimetype"); // determines the meaning of this data1 if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {builder. append ("phone = "). append (data1 ). append (",");} else if ("vnd. android. cursor. item/email_v2 ". equals (mimetype) {builder. append ("email = "). append (data1 ). append (",");} else if ("vnd. android. cursor. item/name ". equals (mimetype) {builder. append ("name = "). append (data1 ). append (",") ;}} perContactsCursor. close ();} allContactsCursor. close (); Log. I (TAG, builder. toString ();} // query the contact name by phone number. public void getNameByTele () {contentResolver = this. getContext (). getContentResolver (); Uri teleUri = Uri. parse ("content: // com. android. contacts/data/phones/filter/12345 "); // The following two statements involve associating the data table and raw_contacts table // Cursor oneContactCursor = contentResolver. query (teleUri, null); // name = oneContactCursor. getString (oneContactCursor. getColumnIndex ("display_name"); Cursor oneContactCursor = contentResolver. query (teleUri, null); String name = ""; while (oneContactCursor. moveToNext () {name = oneContactCursor. getString (oneContactCursor. getColumnIndex ("display_name");} Log. I (TAG, name) ;}// Add a contact to the address book // first, update the raw_contacts table. The table has a _ id, and then update the data table, store the detailed information in this table // It is not necessary to update the data table before updating the raw_contacts table. because the data table has a foreign key raw_contact_id that is consistent with the primary key _ id of the table Raw_contacts. // in addition to the _ id, The raw_contacts table is updated only after the data table is updated. // Step 1: add an empty data entry to the raw_contacts table. to obtain the Automatically increasing primary key _ id // Step 2: update the data in the data table // somebody's codepublic void testAddContact1 () {// The following is the first Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); ContentResolver resolver = this. getContext (). getContentResolver (); ContentValues values = new ContentValues (); Uri insertUri = resolver. insert (uri, values); long contactid = ContentUris. parseId (insertUri); // The following is Step 2 values. clear (); values. put ("raw_contact_id", contactid); values. put (ContactsContract. data. MIMETYPE, StructuredName. CONTENT_ITEM_TYPE); values. put (StructuredName. GIVEN_NAME, "zhangsan"); uri = Uri. parse ("content: // com. android. contacts/data "); resolver. insert (uri, values); values. clear (); values. put ("raw_contact_id", contactid); values. put (ContactsContract. data. MIMETYPE, Phone. CONTENT_ITEM_TYPE); values. put (Phone. NUMBER, "1386595955"); values. put (Phone. TYPE, Phone. TYPE_MOBILE); uri = Uri. parse ("content: // com. android. contacts/data "); resolver. insert (uri, values); values. clear (); values. put ("raw_contact_id", contactid); values. put (ContactsContract. data. MIMETYPE, Email. CONTENT_ITEM_TYPE); values. put (Email. DATA, "test@sina.com"); values. put (Email. TYPE, Email. TYPE_HOME); uri = Uri. parse ("content: // com. android. contacts/data "); resolver. insert (uri, values );} // multiple operations are executed in the same transaction // The preceding operations must be managed using the transaction // somebody's codepublic void testAddContact2 () throws Exception {Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); ContentResolver resolver = this. getContext (). getContentResolver (); ArrayList <ContentProviderOperation> operations = new ArrayList <ContentProviderOperation> (); ContentProviderOperation rawcontactOperation = ContentProviderOperation. newInsert (uri ). withValue ("account_name", null ). build (); operations. add (rawcontactOperation); uri = Uri. parse ("content: // com. android. contacts/data "); ContentProviderOperation dataOperation = ContentProviderOperation. newInsert (uri ). withValueBackReference ("raw_contact_id", 0 ). withValue ("mimetype", "vnd. android. cursor. item/name "). withValue ("data2", "Li Bai "). build (); operations. add (dataOperation); uri = Uri. parse ("content: // com. android. contacts/data "); dataOperation = ContentProviderOperation. newInsert (uri ). withValueBackReference ("raw_contact_id", 0 ). withValue ("mimetype", "vnd. android. cursor. item/phone_v2 "). withValue ("data1", "1386595955 "). withValue ("data2", Phone. TYPE_MOBILE ). build (); operations. add (dataOperation); uri = Uri. parse ("content: // com. android. contacts/data "); dataOperation = ContentProviderOperation. newInsert (uri ). withValueBackReference ("raw_contact_id", 0 ). withValue ("mimetype", "vnd. android. cursor. item/email_v2 "). withValue ("data1", "test@sina.com "). withValue ("data2", Email. TYPE_HOME ). build (); operations. add (dataOperation); resolver. applyBatch ("com. android. contacts ", operations );}}