java 如何借助 Android 中的 Intent 从自己的应用程序通过 Whatsapp 发送文本消息和图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26627699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:16:16  来源:igfitidea点击:

How to Send a Text message and Image through Whatsapp from Own Application with help of Intent in Android?

javaandroidwhatsapp

提问by Ankit

I am not able to send both text message and image through whatsapp . Either , I am able to start that particular contact chat thread without any message or I am only able to send message but not opening that particular contact chat thread.

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

I have followed following link : http://www.whatsapp.com/faq/en/android/28000012

我遵循了以下链接:http: //www.whatsapp.com/faq/en/android/28000012

Sending message through WhatsApp

通过 WhatsApp 发送消息

Send Whatsapp message to specific contact

向特定联系人发送 Whatsapp 消息

But not getting Success :(

但没有成功:(

Can anybody help me with this. How to Send a Text message and Image through whatsapp from Own Application with help of Intent in Android ?

任何人都可以帮我解决这个问题。如何在 Android 中的 Intent 的帮助下通过自己的应用程序通过 whatsapp 发送文本消息和图像?

回答by Bhavita Lalwani

Try using the following solution,

尝试使用以下解决方案,

            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);

EXTRA INFORMATION ABOUT THE PROCESS OF FINDING THE SOLUTION :

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

After reverse engineering WhatsApp, I came across the following snippet of Android manifest,

在对 WhatsApp 进行逆向工程后,我发现了以下 Android 清单片段,

Normal Share intent, uses "SEND" which does not allow you to send to a specific contact and requires a contact picker.

普通共享意图,使用“ SEND”,它不允许您发送给特定联系人并需要联系人选择器。

The specific contact is picked up by Conversation class and uses "SEND_TO" action, but it uses sms body and can not take up image and other attachments.

具体联系人由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>

Digging further, I came across this,

进一步挖掘,我遇到了这个,

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

Finally using a decompiler for ContactPicker and Conversation class,the extra key-value for phone number was found to be "jid".

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

回答by Manan Sharma

Earlier it wasn't possible but since the May '15 update. Checkout :

早些时候这是不可能的,但自 15 年 5 月更新以来。查看 :

try{
    PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    String sendString = "some random string";
    sendIntent.setPackage("com.whatsapp");
    sendIntent.putExtra(Intent.EXTRA_TEXT, sendString);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("image/*");
    startActivity(sendIntent);
} catch (Exception e){
    // some code
}

Here PackageInfo line is just to check if WhatsApp is installed. It throws Exception if not. You can just ignore that if you want to do a normal share (and setPackage also).

这里 PackageInfo 行只是为了检查是否安装了 WhatsApp。如果没有,它会抛出异常。如果您想进行普通共享(以及 setPackage),则可以忽略它。

Also. It is important that the media you want to share has to be publicly available on local storage.

还。您要共享的媒体必须在本地存储上公开可用,这一点很重要。

UPDATE

更新

To send to a specific contact

发送给特定联系人

Uri uri = Uri.parse("smsto:" + "<CONTACT_NUMBER>");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);

As Action Send Tois now allowed.

现在允许操作发送到

回答by Baljeet

I saw many similar questions here by many peoples, and the answer is "Yes, this is possible". You can send messages directly to Whatsapp from your app without opening Whatsapp, and this can be achieved by using Wear Api and RemoteInput method.

我在这里看到很多人有很多类似的问题,答案是“是的,这是可能的”。您可以直接从您的应用程序向 Whatsapp 发送消息,而无需打开 Whatsapp,这可以通过使用 Wea​​r Api 和 RemoteInput 方法来实现。

It is a bit complicated task, so for reference you can use this code. It does the same that you are searching for.

这是一个有点复杂的任务,因此您可以使用此代码作为参考。它的作用与您正在寻找的相同。