转自:https://blog.csdn.net/antimage08/article/details/50449165
需要提前获得读取联系人权限和写入联系人权限(如果不对联系人操作可以不获取)
Android 系统用于管理联系人的 ContentProvider 的几个 Uri:
-----> ContactsContract.Contacts.CONTENT_URI:管理联系人的 Uri。
-----> ContactsContract.CommonDataKinds.Phone.CONTENT_URI:管理联系人电话的 Uri。
-----> ContactsContract.CommonDataKinds.Email.CONTENT_URI:管理联系人 E-mail 的 Uri。
// 使用ContentResolver查找联系人数据
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
// 遍历查询结果,获取系统中所有联系人
while (cursor.moveToNext()) {
// 获取联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的名字
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
names.add(name);
// 使用ContentResolver查找联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
ArrayList<String> detail = new ArrayList<>();
// 遍历查询结果,获取该联系人的多个电话号码
while (phones.moveToNext()) {
// 获取查询结果中电话号码列中数据
String phoneNumber = phones.getString(phones
.getColumnIndex(ContactsContract
.CommonDataKinds.Phone.NUMBER));
detail.add("电话号码:" + phoneNumber);
}
phones.close();
// 使用ContentResolver查找联系人的E-mail地址
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email
.CONTACT_ID + " = " + contactId, null, null);
// 遍历查询结果,获取该联系人的多个E-mail地址
while (emails.moveToNext()) {
// 获取查询结果中E-mail地址列中数据
String emailAddress = emails.getString(emails
.getColumnIndex(ContactsContract
.CommonDataKinds.Email.DATA));
detail.add("邮件地址:" + emailAddress);
}
emails.close();
details.add(detail);
}
cursor.close();