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();