如何通过 WhatsApp 从我的应用程序中单击一下直接向联系人发送消息?

How to directly send a message to a contact through WhatsApp from my app with just one click?

我需要使用 WhatsApp 从我的应用向选定的联系人发送消息。我有联系人姓名、电话号码、ID 和我要发送的消息。只需要能够发送它,而不必在 WhatsApp 或应用程序中选择联系人来共享消息。 我测试了几种方法,但我没有找到任何可行的解决方案,最简单的如下:

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

使用这种方法会自动选择联系人,但随后会在 WhatsApp 中弹出该联系人的对话,最糟糕的是消息甚至不存在。有了消息,我只需要点击发送就可以了,即使我需要一次完成所有事情。

提前致谢!

PD:感谢您的回答是 root,但这是我最后的选择,我只需要能够从非 root 设备发送消息。

你现在还不能这样做,因为 WhatsApp 还没有向开发者开放他们的 API。你能做的最好的就是描述 here.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

这很奇怪,因为显然您可以 select the contact in iOS 通过传递一些额外的参数。

坦率地说,目前没有 public API 可用于共享特定联系人的消息。但是,如果您不关心已获得 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 }' | xargs kill");
    commandsTestOnClick("ps | grep -w 'com.whatsapp' | awk '{print }' | 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();
  }
 } 

经过测试,即使不打开 WhatsApp 也能完美运行!

原始来源,

Whatsapp message share without opening whatsapp