按电话号码检索联系人电话号码、联系人

2023-09-12 08:46:46 作者:一个人的旅行

在我的应用程序,用户写的电话号码,我想找到与该电话号码的联系人姓名?

In my app, user writes a phone number, and I want to find the contact name with that phone number?

我通常寻找这样联系人: 光标CUR = getContentResolver()查询(ContactsContract.Contacts.CONTENT_URI,                 NULL,NULL,NULL,NULL);

I usually search the contacts like this: Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

不过,我这样做是为了获取的所有联系人......在这个程序我只是想获得给定的电话号码,联系人姓名......我怎么能限制查询?

But I do this to access all contacts... In this app I only want to get the contact name of the given phone number... How can I restrict the query?

还是我必须走线槽的所有联系人,看看是否有任何给定的电话号码?但我认为,这可能会很慢这样...

Or do I have to go trough all contacts and see if any has the given phone number? But I believe that this can be very slow this way...

推荐答案

您应该看看推荐的ContactsContract.PhoneLookup供应商

You should have a look at the recommended ContactsContract.PhoneLookup provider

这是重新presents寻找了一个电话号码,例如来电显示的结果的表。要进行查找,你必须附加要找到CONTENT_FILTER_URI的数量。这个查询进行了高度优化。

A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...