Android: How to get contact id by phone number?

I need to do this in Android (API 1.6) donut

+6
source share
1 answer

try this piece of code, it worked fine for me ...

ContentResolver contentResolver = context.getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}; Cursor cursor = contentResolver.query( uri, projection, null, null, null); if(cursor!=null) { while(cursor.moveToNext()){ String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID)); Log.d(LOGTAG, "contactMatch name: " + contactName); Log.d(LOGTAG, "contactMatch id: " + contactId); } cursor.close(); } 
+19
source

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


All Articles