设置读取权限
<uses-permission android:name="android.permission.READ_CONTACTS"
/>
<uses-permission
android:name="android.permission.WRITE_CONTACTS" />
主要代码
public void readContacts() {
Cursor cursor = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, 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);
Toast.makeText(this, name,
1000).show();
Cursor phones =
this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[] { contactId }, null);
if(phones.moveToNext()){
int
phoneIndex=phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String
phoneNumber=phones.getString(phoneIndex);
Toast.makeText(this, phoneNumber, 1000).show();
}
phones.close();
Cursor email =
this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
new String[] { contactId }, null);
if(email.moveToNext()){
int
emailIndex =email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
String
emailAddress=email.getString(emailIndex);
Toast.makeText(this, emailAddress, 1000).show();
}
email.close();
}
}