android读取特定联系人是真的烦, 网上代码一大堆,但是拿过来又不好使,无奈,只有自己一点一点弄了(找资料+测试 = 一天时间)。
首先需要跳转到联系人列表:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, PEOPLE_REQUEST_CODE);
}
接下来重新onActivityResult方法
if(resultCode == RESULT_OK){
if(requestCode == PEOPLE_REQUEST_CODE){
if(null == data){
return;
}
//获得选取的联系人信息
Uri mUri = data.getData();
String [] contact=getPhoneContacts(mUri);
if(contact != null){
Map<String,String> map = new HashMap<String,String>();
map.put(contact[0],contact[1]);
contacts.add(map);
adapter.notifyDataSetChanged();
}
}
}
private String[] getPhoneContacts(Uri uri){
String [] contacts = new String[2];
String[] projectionNumber = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursorNumber = getContentResolver().query(uri, projectionNumber, null, null, null);
Log.e(TAG, "Cursor: "+cursorNumber.getPosition() );
// If the cursor returned is valid, get the phone number
if (cursorNumber != null && cursorNumber.moveToFirst()) {
int numberIndex = cursorNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.e(TAG, "getPhoneContacts: numberIndex"+"-->"+numberIndex );
String number = cursorNumber.getString(numberIndex);
// Do something with the phone number
number = number.replaceAll("-","");
number = number.replaceAll(" ","");
Log.e(TAG, "getPhoneContacts: number"+"-->"+number );
contacts[1] = number;
cursorNumber.close();
}else{
cursorNumber.close();
showMissingPermissionDialog();
return null;
}
String[] projectionName = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursorName = getContentResolver().query(uri, projectionName, null, null, null);
if (cursorName != null && cursorName.moveToFirst()) {
int nameIndex = cursorName.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
Log.e(TAG, "getPhoneContacts: nameIndex"+"-->"+nameIndex );
String name = cursorName.getString(nameIndex);
Log.e(TAG, "getPhoneContacts: name"+"-->"+name );
// Do something with the phone name
contacts[0] = name;
cursorName.close();
}else{
cursorName.close();
showMissingPermissionDialog();
return null;
}
return contacts;
}
/**
* 显示提示信息
*
* @since 2.5.0
*/
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.notifyTitle);
builder.setMessage(R.string.notifyContactMsg);
// 拒绝, 退出应用
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// 设置,管理权限
builder.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
/**
* 启动应用的设置
*
* @since 2.5.0
*/
private void startAppSettings() {
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, SETTING_REQUESTCODE);
}
这是我最后一点一点测试出来的结果,目前华为、小米测试可以获取
最后附上资料链接
https://developer.android.google.cn/guide/components/intents-common.html#Contacts