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

时间:2013-09-19 10:36:49

标签: android whatsapp

我遵循了这个link,这是我的代码

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联系人(没有选择联系号码,我已经知道ID)?如果需要Root

8 个答案:

答案 0 :(得分:7)

我找到了正确的方法,在您阅读本文之后,这很简单:http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/

电话和留言都是字符串。

源代码:

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

享受!

答案 1 :(得分:4)

如果您想在不打开whatsapp的情况下在后台向特定用户发送消息,并且您已经拥有root设备,那么使用以下代码将对您有帮助,

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

请查看此博客以获取更多信息......我已经测试过它是否正常运行

http://siddhpuraamit.blogspot.in/

答案 2 :(得分:2)

谷歌搜索了一下之后,我发现了以下代码

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

}

所以你可以发送一个发送消息的意图,但据我所知,你不能把它发送给特定的联系人

答案 3 :(得分:2)

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

答案 4 :(得分:1)

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

如果您只想发送短信,只需更改setType(“text”)并删除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);

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

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

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

特定联系人由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 ”。

答案 5 :(得分:0)

请尝试以下操作:

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

答案 6 :(得分:-1)

我使用此代码对我来说非常合适。此外,我正在检查是否安装了什么应用程序才能进入商店。

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

答案 7 :(得分:-1)

您可以尝试一下,它对我有用:

String waUrl = "https://wa.me/918982061674?text=I+can+help+you+with+your+BA+Tuition+requirement.+Please+call+me+at+09080976510+or+check+my+UrbanPro+profile%3A+http%3A%2F%2Flocalhost%3A8080%2Ftesturl";

try {
  PackageManager packageManager = context.getPackageManager();
  Intent i = new Intent(Intent.ACTION_VIEW);
  String url = waUrl;
  i.setPackage("com.whatsapp");
  i.setData(Uri.parse(url));
  if (i.resolveActivity(packageManager) != null) {
    context.startActivity(i);
  }
} catch (Exception e) {
  Log.e("WHATSAPP", e.getMessage())
}