androidandroid-1.6-donut

How can I query contact information based on a phone number


I am trying to query contact information based on a phone number on android 1.6. This is the code I tried. But I get count equals to 0 in my cursor.

    String selection = "PHONE_NUMBERS_EQUAL(" + People.Phones.NUMBER + " , "   + phoneNumber + ")";
    Cursor cursor = mContext.getContentResolver().query(People.CONTENT_URI,
            new String[] {People._ID, People.NAME, People.Phones.NUMBER},
            selection, null, null);

Do you have any idea why it is not working?

Thank you.


Solution

  • You can specify a URI to and use a query to directly get a contact info by phone number..

    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));
    
    Cursor cursor = mContext.getContentResolver().query(contactUri, null, null, null, null);
    

    The cursor returned by the code above should then contain the contact you are looking for and you can get the info out you need...

    if(cursor.moveToFirst()){
        int personIDIndex = cursor.getColumnIndex(Contacts.Phones.PERSON_ID);
        //etc
    }