我对Nexus 6(Android 7.0)有一个奇怪的问题,即当通过意图添加新联系人时,Uri会因为rawContacts不像往常一样进行查找。
这是我的意图:
public static Intent getNewContactIntent() {
Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
intent.putExtra("finishActivityOnSaveCompleted", true);
return intent;
}
结果onActivityResult:
Uri contactUri = data.getData();
在Nexus 6(Api 24)设备上:
内容://com.android.contacts/raw_contacts/1376
在其他设备上,包括模拟器上的Nexus 6:
内容://com.android.contacts/contacts/lookup/2883i3c5a4b238cc57aad/1376
如何设法在包括Google在内的所有Android设备上获得相同的结果?
额外信息:
这种奇怪的行为也可以在Pixel(Android 8)上重现。
使用uri检索数据( ... / raw_contacts / 1376 ):
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
| column | value |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
| sort_key | Maher1 |
| send_to_voicemail | 0 |
| pinned | 0 |
| display_name | Maher1 |
| metadata_dirty | 0 |
| phonebook_label_alt | M |
| phonebook_bucket | 13 |
| version | 7 |
| custom_ringtone | null |
| _id | 1376 |
| times_contacted | 0 |
| account_type_and_data_set | com.google |
| sync4 | null |
| dirty | 0 |
| sync2 | Rn48fTVSLi17ImA9XBZXEUUJQwI."" |
| contact_id | 1376 |
| raw_contact_is_user_profile | 0 |
| aggregation_mode | 0 |
| data_set | null |
| phonebook_label | M |
| account_type | com.google |
| sync3 | 2017-08-31T12:44:27.075Z |
| display_name_alt | Maher1 |
| phonetic_name | null |
| last_time_contacted | null |
| display_name_source | 40 |
| backup_id | 3c5a4b238cc57aad |
| phonebook_bucket_alt | 13 |
| sort_key_alt | Maher1 |
| starred | 0 |
| deleted | 0 |
| account_name | maher*********@gmail.com |
| sourceid | 3c5a4b238cc57aad |
| sync1 | https://www.google.com/m8/feeds/contacts/maher*********%40gmail.com/base2_property-android_linksto-gprofiles_highresphotos/3c5a4b238cc57aad |
| phonetic_name_style | 0 |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
虽然可以使用预期的uri检索数据( ... / contacts / lookup / 2883i3c5a4b238cc57aad / 1376 ):
+--------------------------------+-----------------------+
| column | value |
+--------------------------------+-----------------------+
| sort_key | Maher1 |
| photo_uri | null |
| send_to_voicemail | 0 |
| contact_status | null |
| contact_status_label | null |
| pinned | 0 |
| display_name | Maher1 |
| phonebook_label_alt | M |
| phonebook_bucket | 13 |
| contact_status_res_package | null |
| in_default_directory | 1 |
| photo_id | null |
| custom_ringtone | null |
| _id | 1376 |
| times_contacted | 0 |
| phonebook_label | M |
| lookup | 2883i3c5a4b238cc57aad |
| display_name_alt | Maher1 |
| phonetic_name | null |
| last_time_contacted | 0 |
| contact_last_updated_timestamp | 1504183466085 |
| has_phone_number | 0 |
| in_visible_group | 1 |
| photo_file_id | null |
| display_name_source | 40 |
| is_user_profile | 0 |
| contact_status_ts | null |
| sort_key_alt | Maher1 |
| phonebook_bucket_alt | 13 |
| contact_presence | null |
| starred | 0 |
| photo_thumb_uri | null |
| contact_status_icon | null |
| contact_chat_capability | null |
| name_raw_contact_id | 1376 |
| phonetic_name_style | 0 |
+--------------------------------+-----------------------+
答案 0 :(得分:0)
我找到的最佳解决方案:
public static Uri getLookupURI(Intent data, ContentResolver resolver) {
Uri contactData = data.getData();
if (isRawContactsUri(contactData)) {
contactData = ContactsContract.RawContacts.getContactLookupUri(resolver, contactData);
}
return contactData;
}
public static boolean isRawContactsUri(Uri contactUri) {
return null != contactUri.getPathSegments() && !contactUri.getPathSegments().isEmpty() && "raw_contacts".equals(contactUri.getPathSegments().get(0));
}
检查结果是否包含rawContacts,然后提取LookupUri。