以阿卡什的回答为基础。
第 1 步- 需要在 Manifest 中添加读/写联系人权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- OR - depends on the requiremnts-->
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
第 2 步- 然后需要启动联系人选择器意图,但在此之前需要检查是否授予联系人权限(仅适用于 Android >=23)
if (hasPermissions(Manifest.permission.READ_CONTACTS)) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_CONTACT);
} else {
// Request Permissions
}
第 3 步- 然后需要获取活动结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_PICK_CONTACT) {
String phoneNo = null;
String name = null;
Uri uri = data.getData();
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
Log.e("onActivityResult()", phoneIndex + " " + phoneNo + " " + nameIndex + " " + name);
}
cursor.close();
}
}
是的,就是这样。