如何从我的Android应用程序通过WhatsApp向特定联系人发送消息? [英] How can I send message to specific contact through WhatsApp from my android app?

查看:880
本文介绍了如何从我的Android应用程序通过WhatsApp向特定联系人发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,我需要向WhatsApp的特定联系人发送消息。
我试过这段代码:

I am developing an android app and I need to send a message to specific contact from WhatsApp. I tried this code:

Uri mUri = Uri.parse("smsto:+999999999");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);

问题是WhatsApp上没有收到参数sms_body,但选择了联系人。

The problem is that the parameter "sms_body" is not received on WhatsApp, though the contact is selected.

推荐答案

尝试使用 Intent.EXTRA_TEXT 而不是 sms_body 作为您的额外密钥。根据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.

他们的网站

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

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

Their example uses Intent.ACTION_SEND instead of Intent.ACTION_SENDTO, so I'm not sure if WhatsApp even supports sending directly to a contact via the intent system. Some quick testing should let you determine that.

这篇关于如何从我的Android应用程序通过WhatsApp向特定联系人发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆