Get Contact by Phone number on Android

前端 未结 3 2029
刺人心
刺人心 2020-12-20 04:50

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...

T

相关标签:
3条回答
  • 2020-12-20 05:20

    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-20 05:29

    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();
                }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-20 05:31

    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.

    0 讨论(0)
提交回复
热议问题