Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check if a contact on android phone book has whatsapp enabled?

For a given number from my address book, I need to look-up if the number has whatsapp enabled. (The idea is to choose SMS/WhatsApp for initiating a text intent)

Lets say, I have two numbers under a contact, And I need to know which one has whatsapp enabled.

The "People" app on the Nexus 4 shows both contact numbers, And also a little below has a CONNECTIONS section, which shows only the WhatsApp possible contact.

Is there a way to look up(like how People app does) ?

like image 788
user3087095 Avatar asked Dec 10 '13 14:12

user3087095


People also ask

How do you know if a contact has WhatsApp?

If the Person is on WhatsApp, you will see a green check mark and “WhatsApp Account” below the phone number. Now, if the Person is not using WhatsApp, you will see “Not on WhatsApp” below the phone number.

Why do some contacts not show up in WhatsApp?

If you're a WhatsApp user, you may find that when you switch phones or change number, some of your contacts that were previously available do not show up. This is usually because the country code is missing. If you're based in the same country as the person that you are trying to message, this may not be a problem.


Video Answer


2 Answers

If you want to know if this contact has WhatsApp:

String[] projection = new String[] { RawContacts._ID };
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[] { "THE_CONTACT_DEVICE_ID", "com.whatsapp" };
Cursor cursor = activity.getContentResolver().query(RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
boolean hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp){
    String rowContactId = cursor.getString(0);
}

And to find to which number of this contact has WhatsApp

projection = new String[] { ContactsContract.Data.DATA3 };
selection = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ";
selectionArgs = new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId };
cursor = CallAppApplication.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, "1 LIMIT 1");
String phoneNumber = null;
if (cursor.moveToNext()) {
    phoneNumber = cursor.getString(0);
}
like image 80
idog Avatar answered Oct 24 '22 07:10

idog


Using @idog's method, I improved code to work easier. contactID is a string variable to be passed. If contact hasn't WhatsApp returns null, otherwise returns with contactID which has been passed as variable.

public String hasWhatsapp(String contactID) {
    String rowContactId = null;
    boolean hasWhatsApp;

    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
    String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
    Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
    if (cursor != null) {
        hasWhatsApp = cursor.moveToNext();
        if (hasWhatsApp) {
            rowContactId = cursor.getString(0);
        }
        cursor.close();
    }
    return rowContactId;
}
like image 28
Aykut Uludağ Avatar answered Oct 24 '22 06:10

Aykut Uludağ