android - 即使在添加 Intent.FLAG_ACTIVITY_NEW_TASK 后 startActivity 也无法正常工作
问题描述
我的应用程序有一个share
类来共享文件。从目标 27 升级到目标 29 后,它停止使用AndroidRuntimeException
:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
所以我添加了标志,但我仍然得到完全相同的错误。为什么即使设置了标志,我也会收到错误消息?我android:launchMode
在清单文件中没有。
这是已添加标志的方法:
public class Share {
public static void sharefile(Context context, String filename) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.putExtra(Intent.EXTRA_STREAM, (getUriFromFile(new File(filename)));
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, filename);
sharingIntent.setType((new FileChooser()).getMimeType(filename));
context.startActivity(Intent.createChooser(sharingIntent, filename));
}
这就是它的名称:
Share.sharefile(getApplicationContext(), filename);
解决方案
推荐阅读
- c# - Link existing docker to external configuration file and data to be used it in C# core (linux container)
- ios - 目标C:有没有办法在另一个方法中调用一个方法的完成块?
- node.js - Node.js - 如何从 json 文件中获取数据并使用该数据在 xml 中设置自定义属性
- java - Jersey Client 和 CXF 混淆(多部分问题)
- functional-programming - Ocaml如何用尾递归对单个元素求和?
- c# - 语义版本控制和依赖关系更改
- python - Python对象到文件路径?语法错误
- backbone.js - Backbone.js - 在 devTools 中获取 childElementCount 值以进行打印
- java - 给定一个 Class 对象,我如何创建它引用的类类型列表的实例
- c# - 平滑不同幅度的噪声(第 2 部分)