简体   繁体   English

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

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

I am not able to send both text message and image through whatsapp . 我无法通过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. 要么,我能够在没有任何消息的情况下启动该特定的联系人聊天线程,或者我只能发送消息但不能打开该特定的联系人聊天线程。

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发送文本消息和图像?

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. 正常分享意图,使用“ 发送 ”,不允许您发送给特定联系人并需要联系人选择器。

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 ”操作,但它使用sms正文并且无法占用图像和其他附件。

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

Earlier it wasn't possible but since the May '15 update. 早些时候,这是不可能的,但自2015年5月更新以来。 Checkout : 查看 :

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. 这里的PackageInfo行只是检查是否安装了WhatsApp。 It throws Exception if not. 如果没有,它会抛出异常。 You can just ignore that if you want to do a normal share (and setPackage also). 如果你想做一个正常的共享(和setPackage),你可以忽略它。

Also. 也。 It is important that the media you want to share has to be publicly available on local storage. 您要共享的媒体必须在本地存储上公开可用,这一点非常重要。

UPDATE 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 To is now allowed. 现在允许动作发送到

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发送消息,这可以通过使用Wear 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. 它与您正在搜索的内容相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的 Android 应用程序通过 WhatsApp 向特定联系人发送消息? - How can I send message to specific contact through WhatsApp from my android app? 如何在android studio中的whatsapp上自动发送消息 - how to auto send message on whatsapp in android studio 如何通过Instagram,Whatsapp或任何社交应用程序从自己的应用程序共享图像文件? - How to share the image file from your own app through Instagram, Whatsapp or maybe any social app? 如何从我的应用程序发送应用程序邀请(通过 whatsapp、远足、消息) - How to send app invites from my application (via whatsapp,hike,message) 通过意图打开whatsapp在Android 11中不起作用 - Opening whatsapp through intent not working in Android 11 无法通过文本字段将消息从服​​务器发送到android中的客户端 - Unable to send message from server to the client in android through text field 直接从我的 android 应用程序发送 whatsapp 消息 - Send whatsapp message directly from my android app 如何通过Viber消息从Android应用程序发送消息 - How to send message from Android app through Viber message 如何通过Android Intent发送Parse objectId? - How to send a Parse objectId through an Android Intent? 如何在android中通过Intent发送返回值? - How send return value through Intent in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM