Varun Ajay Gupta
Varun Ajay Gupta

Reputation: 349

How to get the name of the contact from his number

I am making an call reader application in which i am able to find out the number from which call is coming but now i want my application to say the name of the contact who is placing the call.I am unable to findout the name of the contact. Can anyone help. Thanks

Upvotes: 4

Views: 174

Answers (2)

Android
Android

Reputation: 9023

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;
}

For more Details Visit this

Upvotes: 4

Anjali
Anjali

Reputation: 206

To get the name of the incoming call number,use

name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NAME));

Upvotes: 1

Related Questions