16

我点击了这个链接,这是我的代码

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如果需要的话

4

8 回答 8

8

我找到了正确的方法,并且在您阅读本文后很简单: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();
        }

享受!

于 2017-08-22T15:46:32.783 回答
3

如果您想在不打开 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/

于 2015-05-11T07:25:03.200 回答
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();
    }

}

因此您可以发送发送消息的意图,但据我所知,您无法将其发送给特定联系人

于 2013-09-21T10:53:33.983 回答
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 是什么应用程序的 uri

 0987654321@s.whatsapp.net
于 2013-09-21T10:55:13.240 回答
2

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

如果您只想发送短信,只需更改 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 ”动作,但它使用短信正文,不能占用图片和其他附件。

 <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 ”。

于 2017-01-23T11:45:50.907 回答
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);
于 2013-09-27T17:43:27.610 回答
-1

我使用这段代码,它对我来说非常完美。而且我正在检查whats-app是否没有安装去玩商店。

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;
}
于 2017-08-11T06:28:31.873 回答
-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())
}
于 2018-10-12T14:45:11.597 回答