首页 > 解决方案 > 即使在添加 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);

标签: androidandroid-intenttaskandroid-context

解决方案


推荐阅读