Android 选择指定联系人数据
2017-11-13 本文已影响0人
_Sisyphus
不需要权限,拿来即用哈~
public static final int REQUEST_SELECT_PHONE_NUMBER = 1;
public void selectContact() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIndex); //联系人姓名
String number = cursor.getString(numberIndex); //联系人号码
Log.e(TAG, "onActivityResult: " + name);
Log.e(TAG, "onActivityResult: " + number);
cursor.close();
}
}
}