【问题标题】:Android Intent to call Whatsapp from codeAndroid Intent 从代码中调用 Whatsapp
【发布时间】:2018-01-13 06:51:27
【问题描述】:

我正在使用此代码直接从我的通话记录应用程序调用 WhatsApp。这很好用,但前提是电话号码包含有效的国家/地区代码。例如,使用 919789006685 调用 WhatsApp 可以,但使用 97890 06685 调用它不起作用。这里 91 是国家代码。

由于我的应用从通话记录中读取电话号码,我应该能够使用任何有效的联系电话号码调用 WhatsApp,而不管存储的电话号码格式如何。用户可能会在他们的联系人中存储不包含国家代码的号码。那么这个问题有解决方法吗?

在我的应用中使用的代码:

Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
String waNumber = contactPhone.replace("+", "");
sendIntent.putExtra("jid", waNumber + "@s.whatsapp.net");
startActivity(sendIntent);

【问题讨论】:

标签: android android-intent whatsapp


【解决方案1】:
  Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/html");
        sharingIntent.setPackage("com.whatsapp");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>https://play.google.com/store/apps/details?id=" + context.getPackageName() + "</p>"));
        context.startActivity(Intent.createChooser(sharingIntent, "Share using"));

【讨论】:

  • 谢谢。但我想为给定的联系人电话号码打开 WhatsApp,而不是通用的操作选择器。
  • 我希望在给定联系人 ID 的情况下打开 WhatsApp 对话聊天窗口。谢谢。
【解决方案2】:

这是完整的解决方案。对于在他们的应用程序中尝试相同的人来说可能很有用。感谢 Sourav Ganguly 提供的链接。

android-make whatsapp call

  1. 检索所选联系人的 ID。
  2. 从 ContactsContract.RawContacts 获取联系人 ID 的所有 RAW 联系人 ID
  3. 查询所有原始联系人的 ContactsContract.Data 表并检索列 ContactsContract.Data._ID
  4. 由于一个联系人在 WhatsApp 上可能有多个电话号码处于活动状态,您可能还需要检查 ContactsContract.Data.DATA3 列以过滤掉您要匹配的电话号码
  5. 如果您想开始 WhatsApp 通话,请使用 vnd.android.cursor.item/vnd.com.whatsapp.profile 和 vnd.android.cursor.item/vnd.com.whatsapp.voip.call 开始 WhatsApp 对话
  6. 使用第 3 步中的 ID 启动 WhatsApp。这是示例代码:

    String data = "content://com.android.contacts/data/" + dataId;
                String type = "vnd.android.cursor.item/vnd.com.whatsapp.profile";
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setDataAndType(Uri.parse(data), type);
                sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
    

【讨论】:

    【解决方案3】:

    特定电话号码的 WhatsApp

    val whatsAppIntent = Intent(Intent.ACTION_VIEW)
    val encodedText = URLEncoder.encode("Helo World", "UTF-8")
    whatsAppIntent.data = Uri.parse("http://api.whatsapp.com/send?phone=$phoneNumber&text=$encodedText")
    

    【讨论】:

    • 这是在 whatsapp 上发布聊天内容......我说得对吗@roni?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多