4

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

Varun Ajay Gupta
  • 349
  • 1
  • 3
  • 11

2 Answers2

4
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

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108
1

To get the name of the incoming call number,use

name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NAME));
Anjali
  • 206
  • 3
  • 10