将Whatsapp消息发送给特定联系人

我跟着这个链接 ,这是我的代码

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + "MYNUMBER@s.whatsapp.net")); i.setPackage("com.whatsapp"); startActivity(i); 

这是我的日志

 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/data/MYNUMBER@s.whatsapp.net pkg=com.whatsapp } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416) at android.app.Activity.startActivityForResult(Activity.java:3351) at android.app.Activity.startActivityForResult(Activity.java:3312) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824) at android.app.Activity.startActivity(Activity.java:3522) at android.app.Activity.startActivity(Activity.java:3490) at com.sieryuu.maidchan.MainActivity.onClick(MainActivity.java:61) at android.view.View.performClick(View.java:4147) at android.view.View$PerformClick.run(View.java:17161) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:4787) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 

我的问题:如何在后台发送文本到whatsapp联系人(没有select联系电话号码,我已经知道了ID)? Root需要

Solutions Collecting From Web of "将Whatsapp消息发送给特定联系人"

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

电话和消息都是string。

源代码:

 try { PackageManager packageManager = context.getPackageManager(); Intent i = new Intent(Intent.ACTION_VIEW); 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(); } 

请享用!

谷歌search后,我发现了下面的代码

  public void onClickWhatsApp(View view) { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "YOUR TEXT HERE"; waIntent.setPackage("com.whatsapp"); if (waIntent != null) { waIntent.putExtra(Intent.EXTRA_TEXT, text);// startActivity(Intent.createChooser(waIntent, "Share with")); } else { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); } } 

所以你可以发送一个意图发送一个消息,但迄今为止我读了你不能发送给一个特定的联系人

如果你想发送消息给后台的特定用户,而不打开WhatsApp的,你有根源设备然后使用下面的代码将帮助你,

  protected void whatsAppSendMessage(String[] paramArrayOfString, String paramString) { try { Shell shell = Shell.startRootShell(); int j = paramArrayOfString.length; for (int i = 0; i < j; i++) { String str3; long l1; long l2; int k; String str1; String str2; Random localRandom = new Random(20L); Log.d("AUTO", "ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill"); commandsTestOnClick("ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill"); str3 = paramArrayOfString[i] + "@s.whatsapp.net"; l1 = System.currentTimeMillis(); l2 = l1 / 1000L; k = localRandom.nextInt(); str1 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"INSERT INTO messages (key_remote_jid, key_from_me, key_id, status, needs_push, data, timestamp, MEDIA_URL, media_mime_type, media_wa_type, MEDIA_SIZE, media_name , latitude, longitude, thumb_image, remote_resource, received_timestamp, send_timestamp, receipt_server_timestamp, receipt_device_timestamp, raw_data, media_hash, recipient_count, media_duration, origin)VALUES ('" + str3 + "', 1,'" + l2 + "-" + k + "', 0,0, '" + paramString + "'," + l1 + ",'','', '0', 0,'', 0.0,0.0,'',''," + l1 + ", -1, -1, -1,0 ,'',0,0,0); \""; str2 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"insert into chat_list (key_remote_jid) select '" + str3 + "' where not exists (select 1 from chat_list where key_remote_jid='" + str3 + "');\""; str3 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"update chat_list set message_table_id = (select max(messages._id) from messages) where chat_list.key_remote_jid='" + str3 + "';\""; Log.d("AUTO", str1); Log.d("AUTO", str2); Log.d("AUTO", str3); shell.add( new SimpleCommand( "chmod 777 /data/data/com.whatsapp/databases/msgstore.db")) .waitForFinish(); shell.add(new SimpleCommand(str1)).waitForFinish(); shell.add(new SimpleCommand(str2)).waitForFinish(); shell.add(new SimpleCommand(str3)).waitForFinish(); } shell.close(); } catch (Exception e) { e.printStackTrace(); } } 

只要看看这个博客了解更多信息…我已经testing它正在成功工作

http://siddhpuraamit.blogspot.in/

 private void openWhatsApp(String id) { Cursor c = getSherlockActivity().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(); } 

哪里id是什么应用程序你喜欢

  0987654321@s.whatsapp.net 

尝试以下方法

 Intent i = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("content://com.android.contacts/data/" + "MYNUMBER@s.whatsapp.net")); i.setPackage("com.whatsapp"); startActivity(i); 

尝试使用以下解决scheme,图像和文字。

只要更改setType(“文本”),并删除ExtraStream,如果你只想发送短信。

  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); 

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

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

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

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

  <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.Conversation" android:theme="@style/Theme.App.CondensedActionBar" android:windowSoftInputMode="stateUnchanged"> <intent-filter> <action android:name="android.intent.action.SENDTO"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="sms"/> <data android:scheme="smsto"/> </intent-filter> </activity> 

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

  <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.ContactPicker" android:theme="@style/Theme.App.NoActionBar"> <intent-filter> <action android:name="android.intent.action.PICK"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="com.whatsapp"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="audio/*"/> <data android:mimeType="video/*"/> <data android:mimeType="image/*"/> <data android:mimeType="text/plain"/> <data android:mimeType="text/x-vcard"/> <data android:mimeType="application/pdf"/> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/> <data android:mimeType="application/msword"/> <data android:mimeType="application/vnd.ms-excel"/> <data android:mimeType="application/vnd.ms-powerpoint"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="audio/*"/> <data android:mimeType="video/*"/> <data android:mimeType="image/*"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="send" android:scheme="whatsapp"/> </intent-filter> <meta-data android:name="android.service.chooser.chooser_target_service" android:value=".ContactChooserTargetService"/> </activity> 

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

我使用这个代码完全适合我。 而且我正在检查如果什么应用程序不安装去玩商店。

 private void openWhatsApp() { String smsNumber = "8801714884544"; boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp"); if (isWhatsappInstalled) { try { Intent sendIntent = new Intent("android.intent.action.MAIN"); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, FormViews.getTexBoxFieldValue(enquiryEditText)); sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); } catch(Exception e) { Toast.makeText(getActivity(), "Error/n" + e.toString(), Toast.LENGTH_SHORT).show(); } } else { Uri uri = Uri.parse("market://details?id=com.whatsapp"); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); Toast.makeText(getActivity(), "WhatsApp not Installed", Toast.LENGTH_SHORT).show(); getActivity().startActivity(goToMarket); } } private boolean whatsappInstalledOrNot(String uri) { PackageManager pm = getActivity().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; }