Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get contact name given a phone number in Android

Tags:

java

android

I am trying to retrieve contact names given the contact phone number. I made a function that should work in all API versions, by I can't make it work in 1.6 and I can't see the problem, maybe someone can spot it?

Note that, I've replaced the API constants for strings so I don't have deprecated warning problems.

public String getContactName(final String phoneNumber) 
{  
    Uri uri;
    String[] projection;

    if (Build.VERSION.SDK_INT >= 5)
    {
        uri = Uri.parse("content://com.android.contacts/phone_lookup");
        projection = new String[] { "display_name" };
    }
    else
    { 
        uri = Uri.parse("content://contacts/phones/filter");
        projection = new String[] { "name" }; 
    } 

    uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = "";

    if (cursor.moveToFirst()) 
    { 
        contactName = cursor.getString(0);
    } 

    cursor.close();
    cursor = null;

    return contactName; 
}
like image 253
PerracoLabs Avatar asked Jul 17 '11 00:07

PerracoLabs


2 Answers

public static String getContactName(Context context, String phoneNumber)
{
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null)
    {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) 
    {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}
like image 91
Afzaal Iftikhar Avatar answered Sep 30 '22 12:09

Afzaal Iftikhar


This seems to work fine in the latest versions:

private String getContactName(Context context, String number) {

    String name = null;

    // define the columns I want the query to return
    String[] projection = new String[] {
            ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup._ID};

    // encode the phone number and build the filter URI
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    // query time
    Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

    if(cursor != null) {
        if (cursor.moveToFirst()) {
            name =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            Log.v(TAG, "Started uploadcontactphoto: Contact Found @ " + number);            
            Log.v(TAG, "Started uploadcontactphoto: Contact name  = " + name);
        } else {
            Log.v(TAG, "Contact Not Found @ " + number);
        }
        cursor.close();
    }
    return name;
}
like image 20
StarNix Avatar answered Sep 30 '22 13:09

StarNix