Search contact by phone number
Asked Answered
E

2

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

Electrostatics answered 14/9, 2010 at 19:13 Comment(3)
Read the documentation about what all those nulls can be replaced with :)Prayer
Also, you want to use CONTENT_FILTER_URI.Prayer
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/…Octavla
A
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,...
Aleydis answered 14/9, 2010 at 20:42 Comment(6)
Thanks. Im trying to call getContentResolver() in my broadcast receiver, but it looks like that function doesnt exist...Electrostatics
Try to prefix the context param you have, so be it context.getContentResolver()Aleydis
What specifically goes into the rest of that query? This answer isn't much more helpful than the already existing documentation.Kristoferkristoffer
There you enumerate other columns from PhoneLookup for return.Aleydis
any idea how to search by phone number or name? Edit: that would be CDK.Phone.CONTENT_FILTER_URI, as it seemsTripitaka
How come it returns a cursor with multiple results in it? Are they multiple contacts?Restharrow
D
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;
}
Dezhnev answered 1/11, 2011 at 13:28 Comment(4)
Thanks! I wouldn't initialize the name variable as null is an appropriate return value when there are no records.Decomposed
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...Restharrow
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?Dezhnev
How to search for multiple phone numbers?Boothe
A
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,...
Aleydis answered 14/9, 2010 at 20:42 Comment(6)
Thanks. Im trying to call getContentResolver() in my broadcast receiver, but it looks like that function doesnt exist...Electrostatics
Try to prefix the context param you have, so be it context.getContentResolver()Aleydis
What specifically goes into the rest of that query? This answer isn't much more helpful than the already existing documentation.Kristoferkristoffer
There you enumerate other columns from PhoneLookup for return.Aleydis
any idea how to search by phone number or name? Edit: that would be CDK.Phone.CONTENT_FILTER_URI, as it seemsTripitaka
How come it returns a cursor with multiple results in it? Are they multiple contacts?Restharrow

© 2022 - 2024 — McMap. All rights reserved.