我必须从Android设备联系人列表中选择联系人。
使用以下代码,我使用设备上的联系人列表检索活动:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, ADD_CONTACT_REQUEST_CODE);
选择联系人后,如何获取所选联系人的LOOKUP_KEY?
相反或检索content://com.android.contacts/contacts/lookup/0r2-334B29432D314D2D29/2
,我需要获得0r2-334B29432D314D2D29
,即LOOKUP_KEY
到目前为止,我只能检索完整的Uri
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
switch (requestCode){
case ADD_CONTACT_REQUEST_CODE:
Uri contactUri = data.getData();
String contactUriString = contactUri.toString();
break;
}
}
}
提前谢谢
答案 0 :(得分:4)
有一个简单的API辅助方法 - getLookupUri:
Uri contactUri = data.getData();
ContentResolver cr = getContentResolver();
Uri lookupUri = ContactsContract.Contacts.getLookupUri(cr, contactUri);
<强>更新强>
要获取LOOKUP_KEY,请执行以下操作:
String projection = String[] { Contacts.LOOKUP_KEY };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null && cursor.moveToNext()) {
String lookupKey = cursor.getString(0);
Log.d(TAG, "key = " + lookupKey);
cursor.close();
}