【问题标题】:How to Send a Text message and Image through Whatsapp from Own Application with help of Intent in Android?如何借助 Android 中的 Intent 从自己的应用程序中通过 Whatsapp 发送短信和图像?
【发布时间】:2014-12-25 00:01:13
【问题描述】:

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

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

Sending message through WhatsApp

Send Whatsapp message to specific contact

但没有成功:(

谁能帮我解决这个问题。 如何在Android Intent的帮助下通过whatsapp从自己的应用程序发送短信和图像?

【问题讨论】:

  • 你不能同时发送。但是你可以分别发送文本和图像。!!
  • @Dhina ,我正在使用 Intent 但没有运气......??没有将图像发送给 whats 应用程序中的特定朋友。注意 Image 和 Number of friend 是通过 intent 传递的。这个怎么做?? .. {_}
  • @Aarastu 我也遇到了同样的问题,这里是我的问题的链接stackoverflow.com/questions/30036992/… :(
  • IntentsharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharedIntent.setType("text/plain"); sharedIntent.putExtra(Intent.EXTRA_TEXT, your_text_here); startActivity(Intent.createChooser(sharingIntent, "分享方式")); }

标签: java android whatsapp


【解决方案1】:

尝试使用以下解决方案,

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

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

在对 WhatsApp 进行逆向工程后,我发现了以下 Android 清单的 sn-p,

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

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

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

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

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

【讨论】:

  • 不错的发现!我试过了,它有效。有什么方法可以自动发送消息吗?目前,它将消息粘贴在框中,我仍然需要点击发送图标才能实际发送消息。非常感谢!
  • 谢谢,到目前为止,我还没有找到在没有发送键的情况下自动发送消息的解决方案。因为我们正在做的是与whatsapp分享消息并让它处理这种情况。如果我能够找到解决方法,我将进一步挖掘并更新。感谢您提出一个有趣的问题。
  • 需要注意的是你必须通过PhoneNumberUtils.stripSeparators([COUNTRY_CODE]+[PHONE_NUMBER])+"@s.whatsapp.net"。 919 的含义并不完全清楚。顺便说一句,它在 2.18.277 版本中仍然有效
【解决方案2】:

早些时候这是不可能的,但自 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
}

这里的 PackageInfo 行只是为了检查是否安装了 WhatsApp。如果没有,它会抛出异常。如果你想做一个普通的分享(和 setPackage 也是),你可以忽略它。

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

更新

发送给特定联系人

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

现在允许发送到作为操作。

【讨论】:

  • 可以参考我之前的回答stackoverflow.com/questions/15462874/…。感谢@ArulxZ 提供了一种更好的包发现方法。此外,仍然无法发送给特定联系人
  • 此外,̶发送给特定联系人仍不可能̶现在有可能>>乌里URI = Uri.parse( “smsto:” + “9888873438”); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
【解决方案3】:

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

这是一个有点复杂的任务,所以作为参考你可以使用this code。它与您正在搜索的功能相同。

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2017-02-15
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多