I have a problem displaying exactly those contacts in my app, which are in the address book of my phone.
From the google contacts apps I found many different versions, how to query these contacts:
Using no selection
Uri uri = ContactsContract.CONTACT.CONTENT_URI; getActivity().getContentResolver().query(uri,PROJECTION, null, null, null);
Using only contacts with visible group == 1
Uri uri = ContactsContract.CONTACT.CONTENT_URI; String selection = Contacts.IN_VISIBLE_GROUP + "=1" getActivity().getContentResolver().query(uri,PROJECTION, selection, null, null);
Or to use the directory query param
Uri uri = Contacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)).build(); getActivity().getContentResolver().query(uri, PROJECTION, null, null, null);
Where as the last delivers the best results but no query is really satisfying! :( I tested all these on different devices but was never be able to get exactly the number of contacts in the phonebook.
Has somebody an idea, why no query works for all devices?
Here are the results from my tests:
=================================================================
| Device \ Selection | Phonebook | No (1) | Visible Group (2) | Directory.Default (3) |
=================================================================
| Galaxy S | 288 | 302 | 288 | 288 |
| Nexus 4 | 61 | 196 | 60 | 61 |
| Xperia Go | 32 | 46 | 32 | 33 |
| Nexus 5 | 109 | 168 | 108 | 109 |
| Galaxy S3 | 124 | 124 | 2 | 124 |
| Xperia Z | 157 | 254 | 156 | 180 |
=================================================================