Skip to content Skip to sidebar Skip to footer

Check Incoming Number Is Stored In Contacts List Or Not Android

In my android app when an incoming call i want to show my custom ui and i am able to do this. No i want to check incoming number is from contacts or not. Below is my code for doing

Solution 1:

Try this code instead (using PhoneLookup.CONTENT_FILTER_URI instead of Phones):

Stringres=null;
    try {
        ContentResolverresolver= ctx.getContentResolver();
        Uriuri= Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Cursorc= resolver.query(uri, newString[]{PhoneLookup.DISPLAY_NAME}, null, null, null);

        if (c != null) { // cursor not null means number is found contactsTableif (c.moveToFirst()) {   // so now find the contact Name
                res = c.getString(c.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
            }
            c.close();
        }
    } catch (Exception ex) {
        /* Ignore */
    }        
    return res;

As docs says ContactsContract.PhoneLookup: 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.

Post a Comment for "Check Incoming Number Is Stored In Contacts List Or Not Android"