我确实有我的联系人ID。有没有办法可以获得联系人的所有细节(借助联系人ID)?
Stack Over流程中已经存在question相同的但是答案似乎已经过时且不再有效。
答案 0 :(得分:1)
使用联系人ID获取联系人详细信息的代码段。
public static ModelContact getContactDetails(final Context context, String contactId) {
ModelContact contact = new ModelContact();
contact.setContactId(contactId);
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data._ID,
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.Data.DATA1,
ContactsContract.Data.DATA2,
ContactsContract.Data.DATA3,
ContactsContract.Data.DATA4,
ContactsContract.Data.DATA5,
ContactsContract.Data.DATA6,
ContactsContract.Data.DATA7,
ContactsContract.Data.DATA8,
ContactsContract.Data.DATA10},
ContactsContract.Data.CONTACT_ID + "=?" + " AND "
+ "(" + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' OR "
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "' OR "
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "' OR "
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "' OR "
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "')",
new String[]{contactId}, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
contact.setDisplayName(displayName);
String rowContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
contact.setRawContactId(rowContactId);
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY));
contact.setLookupKey(lookUpKey);
String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
if (mimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
setPhoneList(cursor, contact);
} else if (mimeType.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
setEmailList(cursor, contact);
} else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
setStructuredName(cursor, contact);
} else if (mimeType.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
setOrganization(cursor, contact);
} else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
setAddress(cursor, contact);
}
}
cursor.close();
}
return contact;
}
有关详细信息,请查看以下链接。
我希望它会对你有所帮助。