繁体   English   中英

在我自己的应用程序中接收 Whatsapp“通过发送聊天”意图

[英]Receive Whatsapp "Send chat via" intent in my own app

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

正确的方法是添加以下intent-filter:

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

阅读聊天内容是这样的:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM