公司开发项目,用的是自己私人的小米手机,获取联系人电话,使用正常。来,直接撸代码:
Uri uri = Uri.
parse(
"content://contacts/people");
Intent intent = new Intent(Intent. ACTION_PICK, uri);
Intent intent = new Intent(Intent. ACTION_PICK, uri);
startActivityForResult(intent, 1);
然后onActivityResult()方法接收返回的数据,看代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (data == null) {
return;
}
return;
}
switch (requestCode) {
case 1:
Uri uri = data.getData();
String phone = getPhone(uri);
tvPhone.setText(endPhone);
break;
}
}
}
}
private String getPhone(Uri uri){
String phone = null;
String name = null;
//得到ContentResolver对象
ContentResolver cr = BaseApp.getAppContext().getContentResolver();
//取得电话本中开始一项的光标
Cursor nameCursor = cr.query(uri, null, null, null, null);
if (nameCursor != null) {
nameCursor.moveToFirst();
//取得联系人姓名
int nameIndex = nameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
name = nameCursor.getString(nameIndex);
//取得电话号码
String ContactId = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
phoneCursor.close();
nameCursor.close();
}
if (StringUtil.isBlank(phone)){
ToastUtil.showToast("获取失败,请检查是否授予权限,或手动输入!");
}else {
//得到ContentResolver对象
ContentResolver cr = BaseApp.getAppContext().getContentResolver();
//取得电话本中开始一项的光标
Cursor nameCursor = cr.query(uri, null, null, null, null);
if (nameCursor != null) {
nameCursor.moveToFirst();
//取得联系人姓名
int nameIndex = nameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
name = nameCursor.getString(nameIndex);
//取得电话号码
String ContactId = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
phoneCursor.close();
nameCursor.close();
}
if (StringUtil.isBlank(phone)){
ToastUtil.showToast("获取失败,请检查是否授予权限,或手动输入!");
}else {
phone = StringUtil.clearPhoneFromt(phone);
}
return phone;
return phone;
}
已经可以成功获取联系人的姓名和代码了~就是这么简单,哈哈。但别忘了在AndroidManifest声明权限(Android 6.0还要记得动态申请隐私权限哦)
<uses-permission android:name="android.permission.READ_CONTACTS"/>
功能实现了,哈哈。但是在测试的时候,发现大部分手机能获得通讯录联系人信息,但是在vivo手机就GG了,不是打开联系人空白,就是直接奔溃~以为是vivo手机才会,查看报错:
Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?????一脸懵逼,什么鬼。谷歌也找不到这问题。来吧,给个正确姿势:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, 1);
哈哈,世界又是如此的美好,bug解决啦啦啦。