Search Contact by Phone Number

How to search contacts by name and phone number?

You should use Phone.CONTENT_FILTER_URI instead of Contacts.CONTENT_FILTER_URI

Docs say:

The filter is applied to display names as well as phone numbers.

Try this:

Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);

Search contact by phone number

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

Search a contact using phone number

You need to apply below code to work in Real Mobile with android version 2.1

    //String msgSender="Mobile No";
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender));
Cursor c = getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
try {
c.moveToFirst();
String displayName = c.getString(0);
} catch (Exception e) {
// TODO: handle exception
}finally{
c.close();
}

in Android 2.1 we have to use ContactsContract and also PhoneLookup should be of ContactsContract.

Make sure you have given below permission.

       <uses-permission android:name="android.permission.READ_CONTACTS"/>

google people api search by phonenumbers

Use the canonicalForm of the phoneNumber without the + sign.

Example:

Sample Image

Request parameter:

Sample Image

Output:

Sample Image



Related Topics



Leave a reply



Submit