如何检查Android手机上的联系人是否启用了whatsapp?

时间:2013-12-10 14:27:28

标签: android android-contacts whatsapp

对于我的地址簿中给定的号码,我需要查看号码是否启用了whatsapp。 (我们的想法是选择SMS / WhatsApp来启动文本意图)

可以说,我在联系人下面有两个号码,而且我需要知道哪个号码启用了whatsapp。

Nexus 4上的“People”应用程序显示了两个联系号码, 还有一个下面有一个CONNECTIONS部分,它只显示WhatsApp可能的联系。

有没有办法查找(比如People app的工作方式)?

3 个答案:

答案 0 :(得分:15)

如果您想知道此联系人是否有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)
}

并找到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);
}

答案 1 :(得分:0)

使用@ idog的方法,我改进了代码以便更轻松地工作。 contactID是要传递的字符串变量。如果联系人没有 WhatsApp 返回 null ,否则返回 contactID ,并已作为变量传递。

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

答案 2 :(得分:0)

public int hasWhatsApp(String contactID) {
        int whatsAppExists = 0;
        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) {
                whatsAppExists = 1;
            }
            cursor.close();
        }
        return whatsAppExists;
    }