android - 如何在所有可能的共享意图中显示我的应用程序并接收它们?
问题描述
我希望我的应用程序可以在各种共享中显示,例如纯文本、图像、视频或任何文件。我也想相应地处理它们。我怎样才能做到这一点?我对共享相关内容完全陌生,但找不到任何合适的文档。
解决方案
添加此意图过滤器以接收所有类型的共享意图。
<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
推荐阅读
- php - 空图像时的图像滑块
- algorithm - 六边形图查找是否有一些网格被包围算法
- c# - 在使用具有潜在作用域依赖关系的 Simpleinjector 时处理 Websharper Remoting Singletons 的最佳方法是什么?
- python - 递归循环没有预期的行为
- graphviz - graphviz dot 输出的边缘聚集在一起
- python-3.x - Python中正则表达式的含义
- python - 在 Python 3 中更改 Windows 10 背景
- java - 将 Spring MVC Web 应用程序转换为独立的桌面软件
- java - 驱动程序无法解决错误,而 WebDriver 是通过 Selenium 和 Java 从不同功能的 public static void main() 外部定义的
- python - allure-pytest:AttributeError:“模块”对象没有属性“附件类型”