从 android 个应用向 Whatsapp 上的特定联系人发送消息

Sending message to a particular contact on Whatsapp from android app

我想从我的 android 应用程序向 WhatsApp 上的特定联系人发送消息。 我也可以这样做,但在发送时要求使用 chrome 和 WhatsApp 打开;我不希望应用询问而是直接打开 Whatsapp。

这会在 Whatsapp 上打开具有指定 phone 号码的指定联系人,并预先填写消息。

这里必须写什么来代替 className 的问号 例如,在打开 facebook 应用程序的情况下,我们写 "sharingIntent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");"

您可以通过添加

将您的意向发送到特定包
intent.setPackage("com.whatsapp");

所以您可以使用提供在 chrome 和 WhatsApp 之间进行选择的相同代码,但添加上面的方法。

Try using Intent.EXTRA_TEXT instead of sms_body as your extra key. Per WhatsApp's documentation, this is what you have to use.

来自他们 website 的示例:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);

他们的示例使用 Intent.ACTION_SEND 而不是 Intent.ACTION_SENDTO,所以我不确定 WhatsApp 是否支持通过 Intent 系统直接发送给联系人。一些快速测试应该可以让您确定这一点。

try this :

sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");