Android P新特性:强制执行 FLAG_ACTIVITY_NEW_TASK 要求

 /**
* Retrieve a PendingIntent that will start a new activity, like calling
* {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
* Note that the activity will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*/
PendingIntent#getActivity

在 Android P 中,您不能从非 Activity 环境中启动 Activity,除非您传递 Intent 标志 FLAG_ACTIVITY_NEW_TASK。 如果您尝试在不传递此标志的情况下启动 Activity,则该 Activity 不会启动,系统会在日志中输出一则消息。

Android P之前的版本,从非Activity环境启动Activity,可以启动,但是会给出一条warning信息

04-20 16:09:23.633 W/ActivityManager( 1783): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cat=[3] flg=0x4800000 pkg=com.test.example cmp=com.test.example/.ui.MainActivity (has extras) }

FLAG_ACTIVITY_NEW_TASK: This flag can not be used when the caller is requesting a result from the activity being launched.
如果通过startActivityForResult启动一个FLAG_ACTIVITY_NEW_TASK的intent,则无法通过onActivityResult获取要启动的component的返回结果;新的intent activity一旦启动,则调用startActivityForResult的activity会立即回调进onActivityResult方法中,且resultCode=Activity.RESULT_CANCELED…

猜你喜欢

转载自blog.csdn.net/dzkdxyx/article/details/80022565