Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a phone number with ContactsContract in Android - function doesn't work

I wrote the following function in order to retrieve one single phone number that belongs to the contact with id "contactID".

The function which is to retrieve the phone number:

private String getContactPhone(String contactID) {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = null;
    String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?";
    String[] selectionArgs = new String[] { contactID };
    String sortOrder = null;
    Cursor result = managedQuery(uri, projection, where, selectionArgs, sortOrder);
    if (result.moveToFirst()) {
        String phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        if (phone == null) {
            result.close();
            return null;
        }
        result.close();
        return phone;
    }
    result.close();
    return null;
}

How this function is called:

ArrayList<Contact> resultContacts = new ArrayList<Contact>();
Cursor result = null;
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Event.CONTACT_ID,
        ContactsContract.CommonDataKinds.Event.START_DATE,
};
String where = ContactsContract.Data.MIMETYPE+" = ? AND "+ContactsContract.CommonDataKinds.Event.TYPE+" = "+ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
result = managedQuery(uri, projection, where, selectionArgs, sortOrder);
while (result.moveToNext()) {
    Long id = result.getLong(result.getColumnIndex(ContactsContract.Contacts._ID));
    String phone = getContactPhone(String.valueOf(id));
    ...
}
...

Unfortunately, it doesn't work. I get null if I call this function with the value that I got from "ContactsContract.Contacts._ID". Why is this so? What is wrong?

Edit: I used to map Contacts._ID to CommonDataKinds.Phone.CONTACT_ID - which didn't work. But now I map Contacts.DISPLAY_NAME to CommonDataKinds.Phone.DISPLAY_NAME and it works suddenly - strange, isn't it? But I would rather like to map the IDs instead of the display names. So the question is still topical. Could this be due to different IDs in those tables? Isn't this why there are lookup IDs?

like image 366
caw Avatar asked Dec 12 '22 06:12

caw


2 Answers

To get the contact id in the first part, you should use:

ContactsContract.Data.CONTACT_ID

instead of:

ContactsContract.Contacts._ID

So the projection should be:

String[] projection = new String[] {
         ContactsContract.Data.CONTACT_ID,
         ContactsContract.CommonDataKinds.Event.CONTACT_ID,
         ContactsContract.CommonDataKinds.Event.START_DATE,
 };

And then of course get the correct row:

Long id = result.getLong(result.getColumnIndex(ContactsContract.Data.CONTACT_ID));
like image 55
tidbeck Avatar answered Dec 14 '22 19:12

tidbeck


You are getting null because you have set your projection to null. The projection is basically the list of columns that you want returned e.g.

String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

Usually, when you find the contact, they may have a list of phone numbers, so you have to use another cursor to iterate through the phone numbers, e.g.

Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);    
while (phones.moveToNext()) 
{    
     phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}

Hope this helps.

like image 28
John J Smith Avatar answered Dec 14 '22 20:12

John J Smith