如何借助 Android 中的 Intent 通过 Whatsapp 从自己的应用程序发送短信和图像?

Ank*_*kit 4 java android whatsapp

我无法通过 Whatsapp 发送短信和图像。或者,我能够在没有任何消息的情况下启动该特定联系人聊天线程,或者我只能发送消息但无法打开该特定联系人聊天线程。

我已点击以下链接: http://www.whatsapp.com/faq/en/android/28000012

通过 WhatsApp 发送消息

发送 Whatsapp 消息给特定联系人

但没有获得成功:(

任何人都可以帮我解决这个问题吗?如何借助 Android 中的 Intent 通过 Whatsapp 从自己的应用程序发送短信和图像?

Bha*_*ani 8

尝试使用以下解决方案,

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