4

I'm trying to write an Android program which can identify if a given contact number is associated with WhatsApp or not. I managed to find out if a particular contact name has WhatsApp account or not. How can I find out which contact corresponding to that contact name has WhatsApp? Currently I'm using the following code:

public void identifyWhatsappContact() {
    Cursor c = ctx.getContentResolver().query(
            RawContacts.CONTENT_URI,
            new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
            RawContacts.ACCOUNT_TYPE + "= ? AND " + StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
            new String[] { "com.whatsapp", "John Doe" },
            null);

    ArrayList<String> myWhatsappContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext())
    {
        myWhatsappContacts.add(c.getString(contactNameColumn));
        Log.v("WHATSAPP", c.getString(contactNameColumn)+"");
    }
}

I'm able to identify if "John Doe" has WhatsApp or not, but I can't figure out which phone number of "John Doe" has WhatsApp (assuming the contact has multiple phone numbers). Can anybody please help me? I've already tried the solution here, but it's not working for me.

Community
  • 1
  • 1
Vinit Shandilya
  • 1,643
  • 5
  • 24
  • 44

1 Answers1

7

This example extract all the WhatsApp numbers of a contact name:

public void getWhatsAppNumbers(String contactName) {
    Cursor cursor1 = getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts._ID},
            ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
            new String[]{"com.whatsapp", contactName},
            null);

    while (cursor1.moveToNext()) {
        String rawContactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.RawContacts._ID));

        Cursor cursor2 = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data.DATA3},
                ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
                new String[]{"vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId},
                null);

        while (cursor2.moveToNext()) {
            String phoneNumber = cursor2.getString(0);

            if (TextUtils.isEmpty(phoneNumber))
                continue;

            if (phoneNumber.startsWith("Message "))
                phoneNumber = phoneNumber.replace("Message ", "");

            Log.d("whatsapp", String.format("%s - %s", contactName, phoneNumber));
        }
    }
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • Thanks a lot! Worked for me. Just a small correction. I didn't find any white space after "Message". So, I simply used: phoneNumber.replace("Message", ""). – Vinit Shandilya May 22 '15 at 18:48
  • sir @mattia Maestrini can you suggest a way for my currently developing android application in which i register user using his phone number(number he enters,may not be device phone number).I want to get the application names(like whatsapp,paytm etc) that are using this phone number.so that i can send the linked application name along with the phone number to server. i posted this question in https://www.reddit.com/r/androiddev/comments/9vi48l/get_name_of_the_applications_linked_to_a_phone/ – Jose Kj Nov 09 '18 at 09:26