tags:

views:

29

answers:

1

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

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...

+1  A: 

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

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,...
Pentium10
Thanks. Im trying to call getContentResolver() in my broadcast receiver, but it looks like that function doesnt exist...
Tiago Costa
Try to prefix the context param you have, so be it context.getContentResolver()
Pentium10