我目前正在Android应用上进行开发,要求我阅读设备上的所有联系人,并根据条件仅选择特定的联系人(只有至少有1个有效手机号码和所有电子邮件地址已联系的联系人)那次联系)。
从我在https://stackoverflow.com/a/19563999/3262731尝试推荐的方法开始,但在有大约800个联系人的测试设备上,检索所有记录然后过滤大约需要17-20秒。
理想情况下,我喜欢将标准构建到一个查询中,该查询连接联系人数据库中的联系人,电话和电子邮件存储表,而不是在我的代码中进行过滤。
有人有什么建议吗?
非常感谢和赞赏!
答案 0 :(得分:0)
根据http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
查询
If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.
答案 1 :(得分:0)
Android文档似乎包含您正在寻找的here内容的信息。
private static final String[] PROJECTION =
{
/*
* The detail data row ID. To make a ListView work,
* this column is required.
*/
Data._ID,
// The primary display name
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
Data.DISPLAY_NAME_PRIMARY :
Data.DISPLAY_NAME,
// The contact's _ID, to construct a content URI
Data.CONTACT_ID
// The contact's LOOKUP_KEY, to construct a content URI
Data.LOOKUP_KEY (a permanent link to the contact
};
return new CursorLoader(getActivity(), contentUri, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
有关如何在文档中定义条件的更多详细信息。我认为这比使用ContentResolver要快。