1

I need to directly get the contact name (if registered in contact list) through the contact number.

I can do the opposite (get number through name) with this code:

ContentResolver cr = getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    "DISPLAY_NAME = '"someone"'", null, null);
            if (cursor.moveToFirst()) {

                String contactId =
                        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                Cursor phones = cr.query(Phone.CONTENT_URI, null,
                        Phone.CONTACT_ID + " = " + contactId, null, null);
                while (phones.moveToNext()) {

                    String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                    int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                    Log.v("TAG3",number);
                    ...
                    }
                }
            }

But I can't honestly do the opposite. Can you please guys help?

Tiago
  • 1,116
  • 5
  • 25
  • 39
  • possible duplicate of [Search contact by phone number](http://stackoverflow.com/questions/3712112/search-contact-by-phone-number) – AlexR Nov 08 '11 at 13:07
  • this is duplicate to http://stackoverflow.com/questions/3712112/search-contact-by-phone-number – AlexR Nov 08 '11 at 13:08
  • Thanks, and sorry. I completely missed. – Tiago Nov 08 '11 at 15:14
  • Try this: http://stackoverflow.com/questions/9323025/how-to-get-contact-name-by-sending-contact-number-in-android – Felipe May 01 '12 at 06:26
  • For the facility of others, I have written a post which contains the whole code to query name, photo, contact ID, etc. with decent explanation. The code contains snippets as found on different answers, but more organized and tested. Hope it helps. Link: http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman May 02 '17 at 15:12

0 Answers0