How to get the name of a contact from his number

I am making a call reading application in which I can find out the number from which the call is being made, but now I want my application to say the name of the contact that places the call. I can not find the name of the contact. Can someone help. Thanks

+4
source share
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;
}

More ... Visit This

+4
source

To get the name of the incoming call number, use

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

Source: https://habr.com/ru/post/1533465/


All Articles