简体   繁体   English

使用 Intent.createChooser 并出现错误:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志

[英]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.

相关问题 虽然已经设置了标志 - 从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志 - Although flag is already set - Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。 这真的是你想要的吗? - Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? RecyclerView 适配器 OnClickListener 崩溃:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志 - RecyclerView Adapter OnClickListener crashes: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag 错误FLAG_ACTIVITY_NEW_TASK标志 - error FLAG_ACTIVITY_NEW_TASK flag 解析活动上下文需要FLAG_ACTIVITY_NEW_TASK标志。 这真的是您想要的吗? - Resolve Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 关于上下文FLAG_ACTIVITY_NEW_TASK的错误 - error about context FLAG_ACTIVITY_NEW_TASK 处理FLAG_ACTIVITY_NEW_TASK的NewIntent - Handling onNewIntent for FLAG_ACTIVITY_NEW_TASK Android FLAG_ACTIVITY_NEW_TASK无效 - Android FLAG_ACTIVITY_NEW_TASK not working 使用FLAG_ACTIVITY_NEW_TASK恢复播放后停止音乐 - Stop music after resume using FLAG_ACTIVITY_NEW_TASK 上下文需要 FLAG_ACTIVITY_NEW_TASK 但我已经设置了那个标志 - Context wants FLAG_ACTIVITY_NEW_TASK but I've already set that flag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM