在Whatsapp中打开对话并填充文本

Bog*_* M. 11 android whatsapp

我想打开WhatsApp一个特定的对话,并用一些字符串填充文本字段.

我拥有的代码和我设法与联系人打开对话:

private void openConversationWithWhatsapp(String e164PhoneNumber){
    String whatsappId = e164PhoneNumber+"@s.whatsapp.net";
    Uri uri = Uri.parse("smsto:" + whatsappId);
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.setPackage("com.whatsapp");

    intent.putExtra(Intent.EXTRA_TEXT, "text");
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    intent.putExtra(Intent.EXTRA_TITLE, "title");
    intent.putExtra(Intent.EXTRA_EMAIL, "email");
    intent.putExtra("sms_body", "The text goes here");
    intent.putExtra("text","asd");
    intent.putExtra("body","body");
    intent.putExtra("subject","subjhect");

    startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

但是文本框中没有内容.我试图查看AndroidManifest.xml文件内部并找到有关其对话活动的以下信息:

<activity android:theme="@style/Theme.App.CondensedActionBar" android:name="com.whatsapp.Conversation" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 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>
Run Code Online (Sandbox Code Playgroud)

有人知道额外使用吗?他们故意阻止这个吗?我在他们的FAQ页面中看到了他们的iOS API .

Com*_*Guy 7

可悲的是,他们没有在android上实现这个功能(也许他们将来会这样做).

我已经问了一个类似的问题,但没有结果.虽然whatsapp会对您在代码中显示的某些意图做出反应,但它们根本不会使用您发送的文本.我想这是出于安全原因,想象一下游戏商店中whatsapp-spammer应用程序的数量......我不知道它在iOS中的实现方式(及其原因)...

接近您的解决方案的两个替代方案是

  1. 发送文本并创建一个选择器(无需键入,只需选择whatsapp)
  2. 打开联系人(需要输入文本,但不需要选择联系人)

  • Google Now与第三方消息传递合作以使其发挥作用.whatsapp肯定没有API. (2认同)

小智 7

2017年更新

基于此Whatsapp常见问题解答

我正在使用这个网址

https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps
Run Code Online (Sandbox Code Playgroud)

其中62是印度尼西亚的拨号代码

然后我使用这样的意图(即时通讯使用kotlin)

val uri = Uri.parse("https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)
Run Code Online (Sandbox Code Playgroud)

用您的号码替换62xxxxx这对我来说很好


Bha*_*ani 5

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

您可以将Type 设置为“text”并删除extra_stream 以仅将其用于发送文本。

            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);
Run Code Online (Sandbox Code Playgroud)

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

在对 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>
Run Code Online (Sandbox Code Playgroud)

进一步挖掘,我发现了这个,

 <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>
Run Code Online (Sandbox Code Playgroud)

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


小智 5

我已经做了!

private void openWhatsApp() {
    String smsNumber = "7****"; //without '+'
    try {
        Intent sendIntent = new Intent("android.intent.action.MAIN");
        //sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent);
    } catch(Exception e) {
        Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)