[英]Using Intent.createChooser and getting error: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
I want to open the share via option from the service class.我想通过服务 class 中的选项打开共享。 It is working fine in Android 7, but in 8+ OS it starts showing它在 Android 7 中运行良好,但在 8+ 操作系统中它开始显示
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. android.util.AndroidRuntimeException:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。 Is this really what you want?这真的是你想要的吗?
I have also included this flag to my Intent, but it's still showing the same error.我也将此标志包含在我的 Intent 中,但它仍然显示相同的错误。
Is there any other way to open share via option from the service class?还有其他方法可以通过服务 class 中的选项打开共享吗?
Intent i = new Intent(Intent.ACTION_SEND);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, rasta); //rasta -> Uri obj
i.setType("image/*");
getApplicationContext().startService(Intent.createChooser(i,"Share karna..."));
Intent.createChooser
creates an Intent
, so you need to set the FLAG_ACTIVITY_NEW_TASK
flag on that intent, eg, Intent.createChooser
创建一个Intent
,因此您需要在该意图上设置FLAG_ACTIVITY_NEW_TASK
标志,例如,
Intent i = new Intent(Intent.ACTION_SEND);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, rasta); //rasta -> Uri obj
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Share karna...");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(chooserIntent);
You were also calling startService
instead of startActivity
- make sure to correct that as well.您还调用了startService
而不是startActivity
- 确保也更正它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.