【问题标题】:Send link to Whatsapp via Android Intent通过 Android Intent 发送到 Whatsapp 的链接
【发布时间】:2014-10-04 06:33:57
【问题描述】:

我正在尝试从我的 Android 应用程序发送一条带有链接的文本消息,以发送到 Whatsapp 或 SMS 消息等聊天应用程序。

这些应用程序不接受 text/html 类型作为 Intent 类型,当我使用 text/plain 类型时,我的消息仅包含主题而没有消息正文。

我见过可以通过 Whatsapp 共享链接的应用,例如 Chrome 和 Dolphin Browser 应用。

这是我的代码:

    @JavascriptInterface
    public void sendMessage(String trip) {
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
        startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}

【问题讨论】:

  • 对我来说(Nexus 5、Android 5.0 上的最新 WhatsApp),EXTRA_SUBJECTEXTRA_TEXT 都包含在共享消息中,格式为“主题 - 额外文本”。我所做的:仅使用EXTRA_TEXT,将整个消息(包括网址)放入其中。但是使用纯文本,跳过所有HTML。

标签: android android-intent android-activity whatsapp


【解决方案1】:
@JavascriptInterface
    public void sendMessage(String trip) {
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
        emailIntent.setType("text/plain");
        startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}

在这里,我只需更改emailIntent.setType("text/plain"); 这条线的位置,它就可以工作了。 您可以在消息应用正文电子邮件应用正文中获取链接。但在这里您只能在邮件应用中获取主题文本,而不是在消息应用中获取主题文本,但您可以在正文中获取链接,从而实现您的目标...

就是这样……

【讨论】:

  • 感谢您的回答。据我了解,上面的代码只会触发whatsapp。我可以通过其他接受文本/纯文本(如 Gmail、SMS 等)的应用程序以某种方式将 Whatsapp 添加到选择器活动中吗?
  • 只需删除 waIntent.setPackage("com.whatsapp");从上面的代码行就是这样......
  • 同时删除 if 条件和 else 部分 :)
  • 这将类似于我的代码,这会导致在通过 whatsapp 发送时发送的消息没有链接...
  • 我也试过你的代码,我也遇到了同样的问题 - 邮件的正文不仅仅是主题...
猜你喜欢
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
相关资源
最近更新 更多