如何从我的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.