• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Contacts NOT on Whatsapp - Android

elmousa68

Lurker
Sep 21, 2014
7
0
In the following code fragment, a contact list is retrieved which has the ACCOUNT_TYPE="com.whatsapp".

Cursor c = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY},
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
new String[]{"com.whatsapp"},
"display_name ASC");

From experimenting with code, I have found that ACCOUNT_TYPE can be several things at once. eg. it can be equal to "com.whatsapp" and "com.google".

When I run a the method c.getCount() on the above cursor, I get 956 contacts. While the same command returns 1305 for "com.google", which is the total number of contacts on the device. All of the contacts are Google contacts while 956 of them are Whatsapp contacts.

I am trying to get the contacts on the device which are NOT Whatsapp contacts. I tried the following:

Cursor c = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY},
ContactsContract.RawContacts.ACCOUNT_TYPE + "!= ?",
new String[]{"com.whatsapp"},
"display_name ASC");

Note : != instead of =

This did NOT work.

I tried:

Cursor c = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY},
ContactsContract.RawContacts.ACCOUNT_TYPE + "!= ? and " +
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
new String[]{"com.whatsapp,"com.google"},
"display_name ASC");
This did not work either.

Please help..

How can I get a list of contact which are NOT on a specific account type (e.g. Whatsapp)?

Muchos Gracias. Danke. :)
 
I'm not really sure about this, but I think you need to create a second Cursor to filter the different types of accounts within the Cursor you made.

I think this is because the contacts in the address book can have different types: com.google is the base for everyone, but then they can have the com.whatsapp type and maybe even the com.telegram type.

That's why when you go to do the query and look for contacts that contain com.whatsapp a minority of them come up, since not all of them have that type as well, while if you look for those that are different from com.whatsapp they all come up, since all of them are at least com.google, including those that are not com.whatsapp.

I wish I could help you more than that, but I'm still quite ignorant on the subject myself.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones