简体   繁体   中英

Get Contact by Phone number on Android

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

What I cant seem to figure out is how to get a contact by phone number...

This is my current piece of code I wrote to test which 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 that i only get a few phone numbers returned, while i expect to get all... What am I doing wrong?

I dont think there is anything wrong with the snippet that you had pasted. Try using a while loop to LOG all the phone numbers.

Regarding your requirement to fetch 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 the _ID to determine the contact.

Why to use PhoneLookup instead of Phone?

  • The PhoneLookup is highly optimised in terms of its searches.

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

  • Comparing the values from Phone will not fetch you any result.

  • It provides various other phone number related info. (HAS_PHONE_NUMBER,TIMES_CONTACTED,DISPLAY_NAME etc)

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

You can use this method to get all your contact 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();
                     } 
                pCur.close();
            }
            }
        }
    }

Use this kind of code:

public void logCallLog(String number)
{
    long dialed;
    String columns[]=new String[] {
            CallLog.Calls._ID,
            CallLog.Calls.NUMBER,
            CallLog.Calls.DATE,
            CallLog.Calls.DURATION,
            CallLog.Calls.TYPE};
    String args[]=new String[1];
    args[0]=number;
    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())
    {
        dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
        if(Me.DEBUG)
            Log.v("CallLog", "Call to number: "+number+", registered at: "+new Date(dialed).toString());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM