Warm tip: This article is reproduced from serverfault.com, please click

android-如何按姓名和电话号码搜索联系人?

(android - How to search contacts by name and phone number?)

发布于 2021-02-10 19:34:59

我正在使用 Contacts.CONTENT_FILTER_URI 来搜索联系人。

Uri contentUri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_FILTER_URI,
                Uri.encode(searchString));

searchstring可以是一个数字或名字。这很好用。
我唯一的问题是结果不包含联系电话号码。

我知道我可以通过查询获得它ContactsContract.Data.CONTENT_URI但是,我想找到一种解决方案,该解决方案可以通过单个查询为我提供联系人姓名和电话号码。

Questioner
Ilya Gazman
Viewed
0
marmor 2021-02-11 04:43:10

你应该使用Phone.CONTENT_FILTER_URI而不是Contacts.CONTENT_FILTER_URI

文档说:

该过滤器适用于显示姓名和电话号码。

试试这个:

Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);