When working with contacts in Android it is convenient to get grip of the user’s Id in the contact database. With the Id you can fetch more information about the contact like thumbnail etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static final String contactIdByPhoneNumber(Context ctx, String phoneNumber) { String contactId = null; if (phoneNumber != null && phoneNumber.length() > 0) { ContentResolver contentResolver = ctx.getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); String[] projection = new String[] { PhoneLookup._ID }; Cursor cursor = contentResolver.query(uri, projection, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID)); } cursor.close(); } } return contactId; } |