Wednesday, June 22, 2011

Lookup caller ID

Using PhoneLookup to find the contact name of a given phone number

It's easy to look up the contact name for a given phone number.
a. Create a content provider uri using PhoneLookup.CONTENT_FILTER_URI with the phone number appended into its path.
b. Query with the uri in (a) with selected return columns including PhoneLookup.DISPLAY_NAME which is the contact name.

Note: Add the user permission to androidmanifest.xml as ...
<uses-permission name="android.permission.READ_CONTACTS">



Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(aPhoneNumber));

Cursor c=getContentResolver().query(
uri,
new String[]{PhoneLookup.DISPLAY_NAME},
null,
null,
null);

while(c.moveToNext()){
String name=c.getString(0);
System.out.println("name="+name);
}
c.close();