From http://www.cnblogs.com/error404/archive/2013/03/12/2956090.html
A program for reading contact information in android, including reading the contact name, mobile phone number, and email address
1: androidmanifest. xml content
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.contacts" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" /></manifest>
It is important to set up a test environment and add the <uses-permission> permission to read and write contact information.
2: main method of reading
/** Read the contact information */Public void testreadallcontacts () {cursor = This. getcontext (). getcontentresolver (). query (contactscontract. contacts. content_uri, null, null); int contactidindex = 0; int nameindex = 0; If (cursor. getcount ()> 0) {contactidindex = cursor. getcolumnindex (contactscontract. contacts. _ id); nameindex = cursor. getcolumnindex (contactscontract. contacts. display_name);} while (cursor. movetonext () {string contactid = cursor. getstring (contactidindex); string name = cursor. getstring (nameindex); log. I (TAG, contactid); log. I (TAG, name);/** find the phone information of the contact */cursor phones = This. getcontext (). getcontentresolver (). query (contactscontract. commondatakinds. phone. content_uri, null, contactscontract. commondatakinds. phone. contact_id + "=" + contactid, null, null); int phoneindex = 0; If (phones. getcount ()> 0) {phoneindex = phones. getcolumnindex (contactscontract. commondatakinds. phone. number);} while (phones. movetonext () {string phonenumber = phones. getstring (phoneindex); log. I (TAG, phonenumber);}/** find the contact's email information */cursor emails = This. getcontext (). getcontentresolver (). query (contactscontract. commondatakinds. email. content_uri, null, contactscontract. commondatakinds. email. contact_id + "=" + contactid, null, null); int emailindex = 0; If (emails. getcount ()> 0) {emailindex = emails. getcolumnindex (contactscontract. commondatakinds. email. data);} while (emails. movetonext () {string email = emails. getstring (emailindex); log. I (TAG, email );}}}
3: Currently, there are two contacts in the mobile phone ,,
This is the contact information of Hellen.
This is the contact information of Mike.
4: test result. The output content on the console is:
You can see that two contacts are output in the console.
//--------------------------------------
Get all group numbers and group names
Cursor groupInfoCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, null);while(groupInfoCursor.moveToNext()) { int idIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups._ID); int titleIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups.TITLE); String id = groupInfoCursor.getString(idIndex); String title = groupInfoCursor.getString(titleIndex);}groupInfoCursor.close();
From http://www.cnblogs.com/jxgxy/archive/2012/07/28/2613069.html
Add two read and write permissions first:
<Uses-permission android: name = "android. permission. READ_CONTACTS"/>
<Uses-permission android: name = "android. permission. WRITE_CONTACTS"/>
Package com. eboy. test;
Import java. util. ArrayList;
Import android. content. ContentProviderOperation;
Import android. content. ContentResolver;
Import android. database. Cursor;
Import android.net. Uri;
Import android. test. AndroidTestCase;
Import android. util. Log;
Public class ContectTest extends AndroidTestCase {
Private static final String TAG = "TestContact ";
// Query the name, phone number, and email address of all contacts.
Public void TestContact () throws Exception {
Uri uri = Uri. parse ("content: // com. android. contacts/contacts ");
ContentResolver resolver = getContext (). getContentResolver ();
Cursor cursor = resolver. query (uri, new String [] {"_ id"}, null );
While (cursor. moveToNext ()){
Int contractID = cursor. getInt (0 );
StringBuilder sb = new StringBuilder ("contractID = ");
Sb. append (contractID );
Uri = Uri. parse ("content: // com. android. contacts/" + contractID + "/data ");
Cursor cursor1 = resolver. query (uri, new String [] {"mimetype", "data1", "data2"}, null );
While (cursor1.moveToNext ()){
String data1 = cursor1.getString (cursor1.getColumnIndex ("data1 "));
String mimeType = cursor1.getString (cursor1.getColumnIndex ("mimetype "));
If ("vnd. android. cursor. item/name". equals (mimeType) {// Yes name
Sb. append (", name =" + data1 );
} Else if ("vnd. android. cursor. item/email_v2". equals (mimeType) {// email
Sb. append (", email =" + data1 );
} Else if ("vnd. android. cursor. item/phone_v2". equals (mimeType) {// mobile phone
Sb. append (", phone =" + data1 );
}
}
Cursor1.close ();
Log. I (TAG, sb. toString ());
}
Cursor. close ();
}
// Query the contact name and email address of the specified phone number
Public void testContactNameByNumber () throws Exception {
String number = "18052369652 ";
Uri uri = Uri. parse ("content: // com. android. contacts/data/phones/filter/" + number );
ContentResolver resolver = getContext (). getContentResolver ();
Cursor cursor = resolver. query (uri, new String [] {"display_name"}, null );
If (cursor. moveToFirst ()){
String name = cursor. getString (0 );
Log. I (TAG, name );
}
Cursor. Close ();
}
// Add contacts and use transactions
Public void testaddcontact () throws exception {
Uri uri = URI. parse ("content: // com. Android. Contacts/raw_contacts ");
Contentresolver resolver = getcontext (). getcontentresolver ();
Arraylist <contentprovideroperation> operations = new arraylist <contentprovideroperation> ();
Contentprovideroperation OP1 = contentprovideroperation. newinsert (URI)
. Withvalue ("account_name", null)
. Build ();
Operations. Add (OP1 );
Uri = URI. parse ("content: // com. Android. Contacts/Data ");
Contentprovideroperation OP2 = contentprovideroperation. newinsert (URI)
. Withvaluebackreference ("raw_contact_id", 0)
. Withvalue ("mimetype", "Vnd. Android. cursor. Item/Name ")
. Withvalue ("data2", "Yong Xiaoyong ")
. Build ();
Operations. Add (OP2 );
Contentprovideroperation OP3 = contentprovideroperation. newinsert (URI)
. Withvaluebackreference ("raw_contact_id", 0)
. Withvalue ("mimetype", "Vnd. Android. cursor. Item/phone_v2 ")
. WithValue ("data1", "13539777967 ")
. WithValue ("data2", "2 ")
. Build ();
Operations. add (op3 );
ContentProviderOperation op4 = ContentProviderOperation. newInsert (uri)
. WithValueBackReference ("raw_contact_id", 0)
. WithValue ("mimetype", "vnd. android. cursor. item/email_v2 ")
. Withvalue ("data1", "asdfasfad@163.com ")
. Withvalue ("data2", "2 ")
. Build ();
Operations. Add (op4 );
Resolver. applybatch ("com. Android. Contacts", operations );
}