Get all contacts who have mobile phone numbers

For querying all the contacts, you can query the RawContacts.CONTENT_URI directly. For getting all the phone numbers, you can query the RawContactsEntity.CONTENT_URI. For getting exact contact with the fixed phone number, you can query from PhoneLookup filter URI.

But if you only want to get all the contacts who have mobile phone numbers, usually you will need to query all the mobile phone numbers, and filter that to remove duplicate contacts (some contacts may have more than 1 mobile phone number). Or you can query all the contacts, and filter that to remove the contacts who have no mobile phone numbers.

Here’s my code that work:

private void test() {
    Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
                 new String[]{"_id"}, "contact_id in (select raw_contact_id from data where mimetype_id=5"
                +" AND data2="+CommonDataKinds.Phone.TYPE_MOBILE
                +" )"
                , null, null);
    while (c.moveToNext()) {
        Log.e("Evan",c.getString(0));
    }
    c.close();
}

I think putting “distinct(_id)” in the column strings should also work, and it’s easier.

About Evan

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!