【问题标题】:How to Check if a contact on android phone book has whatsapp enabled?如何检查android电话簿上的联系人是否启用了whatsapp?
【发布时间】:2013-12-28 02:57:55
【问题描述】:

对于我通讯录中的给定号码,我需要查找该号码是否启用了 whatsapp。 (想法是选择 SMS/WhatsApp 来启动文本意图)

假设我在一个联系人下有两个号码,我需要知道哪个号码启用了whatsapp。

Nexus 4 上的“人脉”应用会显示两个联系号码, 下方还有一个 CONNECTIONS 部分,仅显示 WhatsApp 可能的联系人。

有没有办法查找(就像 People 应用程序那样)?

【问题讨论】:

    标签: android android-contacts whatsapp


    【解决方案1】:

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

    【讨论】:

    • 什么是“THE_CONTACT_DEVICE_ID”?
    • 联系人表中用户的contact_id
    • 用电话号码怎么办
    • 您好,我有一个科尔多瓦/电容器应用程序。我有机会在 Cordova 应用程序中实现此代码吗?
    【解决方案2】:

    使用@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;
    }
    

    【讨论】:

      【解决方案3】:
      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;
          }
      

      【讨论】:

      • 此解决方案无效,请更新答案。
      猜你喜欢
      • 2014-05-27
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2014-02-09
      • 1970-01-01
      • 2011-08-24
      相关资源
      最近更新 更多