Get contact by phone number on Android

I know how I can get all contacts in Android, and how to get their phone number.

I can't figure out how to get a contact by phone number ...

This is my current piece of code I wrote to check what phone numbers are available:

// Create a cursor
    Cursor cursor = Base.contentResover().query(Phone.CONTENT_URI, null, null,
            null, null);

    if (cursor.moveToNext()) {
        Log.d("CALLOG", cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)));


The problem is I only get a few numbers returned while I expect to get all ... What am I doing wrong?


source to share

3 answers

I don't think there is anything wrong with the snippet you pasted in. Try using a while loop to record all phone numbers.

Regarding your requirement to get a contact by phone number. Try using the following snippet

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup._ID...


Use _ID to identify the contact.

Why use PhoneLookup instead of a phone?

  • PhoneLookup is optimized in terms of its searches.

  • Phone numbers can be entered into the contacts database with fillers like "(", ")", "-", etc. PhoneLookup
    helps you separate these fillers and only compare the phone number.

  • Comparing the values ​​from the phone will not produce any results.

  • It provides various other phone numbers related information. (HAS_PHONE_NUMBER, TIMES_CONTACTED, DISPLAY_NAME, etc.)

Hope it helps. Let me know if you need anything else.



You can use this method to get all your contacts with name,_id and no.

. You can call this method in your activity.

private void displayContacts() {

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                         System.out.println("name"+name+"ph no"+phoneNo);
                         Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();




Use this type of code:

public void logCallLog(String number)
    long dialed;
    String columns[]=new String[] {
    String args[]=new String[1];
    Cursor c;
    c = this.managedQuery(Uri.parse("content://call_log/calls"),
            columns, CallLog.Calls.NUMBER+"=?", args, "Calls._ID DESC"); //last record first
    while (c.moveToNext())
            Log.v("CallLog", "Call to number: "+number+", registered at: "+new Date(dialed).toString());




All Articles