如何通过WhatsApp直接从我的应用程序发送消息,只需点击一下?

时间:2015-07-04 18:13:49

标签: android whatsapp sendmessage

我需要使用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设备发送消息。

2 个答案:

答案 0 :(得分:1)

你现在不能这样做,因为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传递一些额外的参数。

答案 1 :(得分:1)

坦率地说,目前没有公共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 $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();
  }
 } 

即使没有打开WhatsApp,也经过了测试和完美测试!

原始资料,

Whatsapp message share without opening whatsapp