Failed to identify WhatsApp contacts

I am trying to write an Android program that can detect if a given contact number is associated with WhatsApp or not. I was able to figure out if a particular contact name has a WhatsApp account or not. How do I know which contact matches that WhatsApp contact name? I am currently 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 can tell if "John Doe" has WhatsApp or not, but I cannot figure out which phone number "John Doe" has WhatsApp (assuming the contact has multiple phone numbers). Can anyone help me? I've already tried the solution here , but it doesn't work for me.

+3


source to share


1 answer


In this example, output all WhatsApp numbers of the contact's 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));
        }
    }
}

      

+6


source







All Articles