Android get name of user using its phone number from phonebook




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 phoneNumber;
        }
        String contactName = null;
        if(cursor.moveToFirst()) {
            contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }
        else
        {
             return phoneNumber;
        }
        if(cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        Log.e("Name and number", contactName+" : "+phoneNumber);
       
        return contactName;
    }

Comments