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

Sufian
  • 6,405
  • 16
  • 66
  • 120
Tiago Costa
  • 4,151
  • 12
  • 36
  • 54
  • Read the documentation about what all those nulls can be replaced with :) – Christopher Orr Sep 14 '10 at 20:16
  • Also, you want to use `CONTENT_FILTER_URI`. – Christopher Orr Sep 14 '10 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: http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman May 02 '17 at 15:12

2 Answers2

89

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;
}
Felipe
  • 16,649
  • 11
  • 68
  • 92
  • Thanks! I wouldn't initialize the `name` variable as `null` is an appropriate return value when there are no records. – Tom Susel Apr 15 '14 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... – android developer Jan 04 '15 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 Jan 05 '15 at 17:43
  • 1
    How to search for multiple phone numbers? – Shajeel Afzal Mar 10 '15 at 08: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,...
Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
Pentium10
  • 204,586
  • 122
  • 423
  • 502