Get Contact by Phone number on Android

2019-01-27 10:22发布

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?

3条回答
够拽才男人
2楼-- · 2019-01-27 10:39

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.

查看更多
等我变得足够好
3楼-- · 2019-01-27 10:40

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());
    }
}
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-27 10:44

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();
            }
            }
        }
    }
查看更多
登录 后发表回答