【问题标题】:Receive Whatsapp "Send chat via" intent in my own app在我自己的应用程序中接收 Whatsapp“发送聊天”意图
【发布时间】:2018-12-30 21:10:03
【问题描述】:

我正在构建一个应用程序,它会要求用户将聊天从 WhatsApp 导出到我的应用程序中。 如何在“通过..发送聊天”意图窗口中显示我的应用?

【问题讨论】:

标签: android android-intent callback whatsapp


【解决方案1】:

这样做的正确方法是添加以下意图过滤器:

    <intent-filter>
        <action android:name="android.intent.action.SENDTO"/>
        <data android:scheme="mailto"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <data android:mimeType="*/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <data android:mimeType="*/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

然后您可以使用以下命令阅读聊天内容:

            Uri uri = intent.getClipData().getItemAt(i).getUri();
            InputStream inputstream = getContentResolver().openInputStream(uri);
            byte[] data = new byte[1024];
            int bytesRead = inputstream.read(data);

            while (bytesRead != -1) {
                chatContent.append(new String(data));
                bytesRead = inputstream.read(data);
            }

            // TODO - Here we can do whatever we want with the chat content chatContent.toString()
            if (mainTextView != null){
                mainTextView.setText(chatContent.toString());
            }

【讨论】:

  • 谢谢!!你能帮我解决这个问题吗stackoverflow.com/questions/55340941/…
  • @Javi 我应该在哪里添加这些意图过滤器?在我的清单中?你通常是在哪里添加的?
  • 谢谢!使用意图过滤器,我的应用程序显示为接收聊天导出,但我无法管理在我的应用程序中接收聊天的意图。我的目标是在手机内存中保存一个 txt 文件,有什么建议吗?
  • 像我一样检查你是否拥有所有权限。
【解决方案2】:

阅读聊天内容是这样的完整:

Intent intent = getIntent();
String type=intent.getType();
    
String action=intent.getAction();
StringBuffer chatContent=new StringBuffer();
// Figure out what to do based on the intent type
if(intent.ACTION_SEND_MULTIPLE.equals(action) && type!=null){
    Bundle bundle=intent.getExtras();
    try {
        for(int i=0;i<intent.getClipData().getItemCount();i++){
            Uri uri = intent.getClipData().getItemAt(i).getUri();
            InputStream inputstream = getContentResolver().openInputStream(uri);
            byte[] data = new byte[1024];
            int bytesRead = inputstream.read(data);

            while (bytesRead != -1) {
                chatContent.append(new String(data));
                bytesRead = inputstream.read(data);
            }
    }
    //aqui se hace lo que se quiera con el chat "chatContent"
    System.out.println(chatContent.toString());
            
    System.out.println(bundle.getString(Intent.EXTRA_TEXT).replaceAll("El historial del chat se adjuntó a este correo como \"Chat de WhatsApp con ","").replaceAll("\".",""));
    }catch (Exception e){
        e.printStackTrace();
    }
}

【讨论】:

  • 这篇文章没有回答这个问题。尽管它有用且相关,但还不是答案。这可能是一条评论,如果它太大而无法放入评论中,则带有指向 sn-p 的链接。请考虑删除您的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多