30

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

3
  • Read the documentation about what all those nulls can be replaced with :) Commented Sep 14, 2010 at 20:16
  • Also, you want to use CONTENT_FILTER_URI. Commented Sep 14, 2010 at 20:17
  • For the facility of others, I have written a post which contains the whole code to query name, photo, contact ID, etc. with decent explanation. The code contains snippets as found on different answers, but more organized and tested. Hope it helps. Link: hellafun.weebly.com/home/…
    – Usman
    Commented May 2, 2017 at 15:12

2 Answers 2

90

If you want the complete code:

public String getContactDisplayNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String name = "?";

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
            ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}
4
  • Thanks! I wouldn't initialize the name variable as null is an appropriate return value when there are no records.
    – Tom Susel
    Commented Apr 15, 2014 at 17:33
  • 6
    What about the special cases, where the user entered a partial number , yet the numbers stored in a format that doesn't match it? For example, in Israel the country prefix is "+972" , and for some cell phone numbers, you add "050", but if it's the full number, it becomes "97250" ( without the first "0") . So, if the user types "050" (to search all phone numbers that has it or at least start with it) , it won't get any result... Commented Jan 4, 2015 at 10:19
  • In my case, I implemented a function to filter a raw number and make all possible combinations, and then I search one by one. Unfortunately I think Android does not have a way to simplify this. Am I wrong?
    – Felipe
    Commented Jan 5, 2015 at 17:43
  • 1
    How to search for multiple phone numbers? Commented Mar 10, 2015 at 8:20
34

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,...
6
  • Thanks. Im trying to call getContentResolver() in my broadcast receiver, but it looks like that function doesnt exist... Commented Sep 14, 2010 at 20:56
  • 3
    Try to prefix the context param you have, so be it context.getContentResolver()
    – Pentium10
    Commented Sep 15, 2010 at 8:25
  • 3
    What specifically goes into the rest of that query? This answer isn't much more helpful than the already existing documentation. Commented Mar 19, 2011 at 21:24
  • There you enumerate other columns from PhoneLookup for return.
    – Pentium10
    Commented Mar 19, 2011 at 21:47
  • any idea how to search by phone number or name? Edit: that would be CDK.Phone.CONTENT_FILTER_URI, as it seems
    – njzk2
    Commented Mar 26, 2012 at 16:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.