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

前端 未结 3 497
醉话见心
醉话见心 2021-01-30 18:35

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

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 19:36

    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);
    }
    

提交回复
热议问题