【问题标题】:Unable to identify WhatsApp contacts无法识别 WhatsApp 联系人
【发布时间】:2015-08-04 09:36:03
【问题描述】:

我正在尝试编写一个 Android 程序,它可以识别给定的联系号码是否与 WhatsApp 相关联。我设法找出一个特定的联系人姓名是否有 WhatsApp 帐户。如何找出与该联系人姓名对应的哪个联系人有 WhatsApp?目前我正在使用以下代码:

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

我能够确定“John Doe”是否有 WhatsApp,但我无法确定“John Doe”的哪个电话号码有 WhatsApp(假设联系人有多个电话号码)。有人可以帮我吗?我已经尝试过here 的解决方案,但它对我不起作用。

【问题讨论】:

  • 我不明白投反对票!

标签: android android-contacts whatsapp android-cursor rawcontactid


【解决方案1】:

此示例提取联系人姓名的所有 WhatsApp 号码:

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

【讨论】:

  • 非常感谢!为我工作。只是一个小小的修正。 “消息”之后我没有找到任何空白。所以,我只是简单地使用了:phoneNumber.replace("Message", "")。
  • sir @mattia Maestrini 您能否为我目前正在开发的 android 应用程序建议一种方法,在该应用程序中我使用他的电话号码(他输入的号码,可能不是设备电话号码)注册用户。我想得到使用此电话号码的应用程序名称(如 whatsapp、paytm 等)。这样我就可以将链接的应用程序名称与电话号码一起发送到服务器。我在reddit.com/r/androiddev/comments/9vi48l/… 中发布了这个问题
猜你喜欢
  • 2017-03-10
  • 2019-01-23
  • 2015-12-21
  • 2017-09-07
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多