以编程方式发送文本到特定的联系人(whatsapp)

我想知道如何发送文本到特定的WhatsApp联系人。 我发现了一些代码来查看特定的联系人,但不发送数据。

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?", new String[] { id }, null); c.moveToFirst(); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0))); startActivity(i); c.close(); 

这工作正常查看whatsapp联系,但我怎么能现在添加一些文本? 或者Whatsapp-developer没有实现这样的api?

我认为答案是你的问题和这个答案在这里的组合: https : //stackoverflow.com/a/15931345/734687所以我会尝试下面的代码:

  1. 将ACTION_VIEW更改为ACTION_SENDTO
  2. 像你一样设置Uri
  3. 将包设置为whatsapp
 Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0))); i.setType("text/plain"); i.setPackage("com.whatsapp"); // so that only Whatsapp reacts and not the chooser i.putExtra(Intent.EXTRA_SUBJECT, "Subject"); i.putExtra(Intent.EXTRA_TEXT, "I'm the body."); startActivity(i); 

我看着WhatsApp的清单,看到ACTION_SEND注册到活动ContactPicker ,所以这不会帮助你。 但是,ACTION_SENDTO已注册到活动com.whatsapp.Conversation ,这听起来更适合您的问题。

WhatsApp可以作为发送短信的替代品,所以它应该像SMS一样工作。 当你不指定所需的应用程序(通过setPackage )Android显示应用程序select器。 因此,您应该查看通过意向发送SMS的代码,然后提供其他包信息。

 Uri uri = Uri.parse("smsto:" + smsNumber); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra("sms_body", smsText); i.setPackage("com.whatsapp"); startActivity(i); 

首先尝试将意图ACTION_SENDreplace为ACTION_SENDTO 。 如果这不能提供额外的额外sms_body 。 如果这不起作用,就试着改变uri。

更新我试图自己解决这个问题,无法find解决scheme。 WhatsApp打开聊天logging,但不会将文本发送。 看来这个function还没有实现。

我已经做了!

 private void openWhatsApp() { String smsNumber = "7****"; // E164 format without '+' sign Intent sendIntent = new Intent(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"); if (intent.resolveActivity(getActivity().getPackageManager()) == null) { Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show(); return; } startActivity(sendIntent); } 

看看我的回答: https : //stackoverflow.com/a/40285262/5879376

  Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation")); sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"@s.whatsapp.net");//phone number without "+" prefix startActivity(sendIntent); 

伟大的黑客Rishabh,非常感谢,我从过去3年来一直在寻找这个解决scheme。

根据上面的Rishabh Maurya的回答,我已经实现了这个在WhatsApp上共享文本和图像的代码。

请注意,在这两种情况下,它都会打开一个whatsapp对话(如果toNumber存在于用户的whatsapp联系人列表中),但用户必须单击sendbutton完成操作。 这意味着它有助于跳过联系人select步骤。

用于短信

 String toNumber = "+91 98765 43210"; // contains spaces. toNumber = toNumber.replace("+", "").replace(" ", ""); Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net"); sendIntent.putExtra(Intent.EXTRA_TEXT, message); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setPackage("com.whatsapp"); sendIntent.setType("text/plain"); startActivity(sendIntent); 

用于分享图片

 String toNumber = "+91 98765 43210"; // contains spaces. toNumber = toNumber.replace("+", "").replace(" ", ""); Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile)); sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net"); sendIntent.putExtra(Intent.EXTRA_TEXT, message); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setPackage("com.whatsapp"); sendIntent.setType("image/png"); context.startActivity(sendIntent); 

享受WhatsApping!

它可以让你打开WhatsApp对话屏幕,为你正在尝试通信的特定用户:

 private void openWhatsApp() { String smsNumber = "91XXXXXXXX20"; boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp"); if (isWhatsappInstalled) { Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation")); sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");//phone number without "+" prefix startActivity(sendIntent); } else { Uri uri = Uri.parse("market://details?id=com.whatsapp"); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show(); startActivity(goToMarket); } } private boolean whatsappInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; } 

我find了正确的方法来做到这一点,并阅读本文后,只是简单的: http : //howdygeeks.com/send-whatsapp-message-unsaved-number-android/

电话和消息都是string。

源代码:

  PackageManager packageManager = context.getPackageManager(); Intent i = new Intent(Intent.ACTION_VIEW); try { String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8"); i.setPackage("com.whatsapp"); i.setData(Uri.parse(url)); if (i.resolveActivity(packageManager) != null) { context.startActivity(i); } } catch (Exception e){ e.printStackTrace(); } 

请享用!

检查这个答案。 这里你的号码以“91 **********”开头。

 Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.putExtra("jid",PhoneNumberUtils.stripSeparators("91**********") + "@s.whatsapp.net"); sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); 

试试这个,为我工作! 。 只是使用意图

  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(whatsappUrl())); startActivity(intent); 

build立whatsappurl。 在whatsapp电话号码中添加国家/地区代码https://countrycode.org/

 public static String whatsappUrl(){ final String BASE_URL = "https://api.whatsapp.com/"; final String WHATSAPP_PHONE_NUMBER = "628123232323"; //'62' is country code for Indonesia final String PARAM_PHONE_NUMBER = "phone"; final String PARAM_TEXT = "text"; final String TEXT_VALUE = "Hello, How are you ?"; String newUrl = BASE_URL + "send"; Uri builtUri = Uri.parse(newUrl).buildUpon() .appendQueryParameter(PARAM_PHONE_NUMBER, WHATSAPP_PHONE_NUMBER) .appendQueryParameter(PARAM_TEXT, TEXT_VALUE) .build(); return buildUrl(builtUri).toString(); } public static URL buildUrl(Uri myUri){ URL finalUrl = null; try { finalUrl = new URL(myUri.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } return finalUrl; } 

这将首先search指定的联系人,然后打开聊天窗口。

注意: phone_numberstr是variables。

 Uri mUri = Uri.parse("https://api.whatsapp.com/send? phone=" + phone_no + "&text=" + str); Intent mIntent = new Intent("android.intent.action.VIEW", mUri); mIntent.setPackage("com.whatsapp"); startActivity(mIntent); 
  private void openWhatsApp() { //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("jid",whatsappId); sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); } catch(Exception e) { Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show(); Log.e("Error",e+"") ; } }