首页 > 解决方案 > 如何在所有可能的共享意图中显示我的应用程序并接收它们?

问题描述

我希望我的应用程序可以在各种共享中显示,例如纯文本、图像、视频或任何文件。我也想相应地处理它们。我怎样才能做到这一点?我对共享相关内容完全陌生,但找不到任何合适的文档。

标签: androidsharingandroid-sharing

解决方案


添加此意图过滤器以接收所有类型的共享意图。

<activity
    android:name=".YourActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="*/*" />
    </intent-filter>
</activity>

在你的接收活动中

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
    if ("text/plain".equals(type)) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    } else {
        Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    }
}

您将使用上述代码获得共享文本和文件 Uri


推荐阅读