在WhatsApp中打开对话并填充文本

我想打开WhatsApp到一个特定的对话,并用一些字符串填充文本字段。

代码,我有,我设法打开与联系人的谈话:

 private void openConversationWithWhatsapp(String e164PhoneNumber){ String whatsappId = e164PhoneNumber+"@s.whatsapp.net"; Uri uri = Uri.parse("smsto:" + whatsappId); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.setPackage("com.whatsapp"); intent.putExtra(Intent.EXTRA_TEXT, "text"); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.putExtra(Intent.EXTRA_TITLE, "title"); intent.putExtra(Intent.EXTRA_EMAIL, "email"); intent.putExtra("sms_body", "The text goes here"); intent.putExtra("text","asd"); intent.putExtra("body","body"); intent.putExtra("subject","subjhect"); startActivity(intent); } 

文本框中没有填充内容。 我试图偷看AndroidManifest.xml文件,发现他们的对话活动的以下信息:

          

有人知道额外使用? 他们故意阻止这个吗? 我在他们的常见问题页面看到了iOS的API。

可悲的是,他们没有在android上实现这个function (也许他们在未来)。

我已经提出了类似的问题 ,但没有结果。 虽然whatsapp会对您在代码中显示的内容有所反应,但它们不会使用您发送的文本。 我想这是因为安全的原因,想象一下whatsapp-spammer应用程序在Play商店中的数量……我不知道它是如何(以及为什么)在iOS中实现的…

接近你的解决方案的两个选择

  1. 发送文本并创建一个选择器 (不需要输入,只需选择whatsapp)
  2. 打开联系人 (文本需要输入,但不需要选择联系人)

我已经做了!

 private void openWhatsApp() { String smsNumber = "7****"; //without '+' try { Intent sendIntent = new Intent("android.intent.action.MAIN"); //sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation")); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); } catch(Exception e) { Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show(); } } 

尝试使用以下解决方案发送图像和文本。

您可以将setType设置为“text”并删除extra_stream以仅用于发送文本。

  Intent sendIntent = new Intent("android.intent.action.SEND"); File f=new File("path to the file"); Uri uri = Uri.fromFile(f); sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker")); sendIntent.setType("image"); sendIntent.putExtra(Intent.EXTRA_STREAM,uri); sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"@s.whatsapp.net"); sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image"); startActivity(sendIntent); 

有关寻找解决方案过程的额外信息:

在逆向工程WhatsApp之后,我遇到了以下Android清单片段,

正常分享意图,使用“ 发送 ”,不允许您发送到特定的联系人,并需要联系人选择器。

具体的联系方式由Conversation类拾取,使用“ SEND_TO ”动作,但使用sms主体,不能占用图片等附件。

           

进一步挖掘,我碰到这个,

                                       

最后在ContactPicker和Conversation类中使用反编译器,发现电话号码的额外键值是“ jid ”。

您可以使用WhatsApp的以下片段:

 public void onClickWhatsApp(View view) { PackageManager pm=getPackageManager(); try { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "YOUR TEXT HERE"; PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA); //Check if package exists or not. If not then code //in catch block will be called waIntent.setPackage("com.whatsapp"); waIntent.putExtra(Intent.EXTRA_TEXT, text); startActivity(Intent.createChooser(waIntent, "Share with")); } catch (NameNotFoundException e) { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); } } 

2017年更新

基于这个WhatsApp的常见问题

即时通讯使用此url

 https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps 

其中62是印度尼西亚的拨号代码

然后我使用这样的意图(即时通讯使用kotlin)

 val uri = Uri.parse("https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps") val intent = Intent(Intent.ACTION_VIEW, uri) startActivity(intent) 

用你的号码替换62xxxxx这个工作对我来说很好