Android初学习 - 在BroadcastReceiver中启动Activity的问题
来源:互联网 发布:每日工作计划软件 编辑:程序博客网 时间:2024/04/29 21:13
在BroadcastReceiver中启动Activity:
如果在BroadcastReceiver的onReceive()方法中如下启动一个Activity
Intent intent=new Intent(context,AnotherActivity.class);
context.startActivity(intent);
可捕获异常信息:
android.util.AndroidRuntimeException:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
Is this really what you want?
它说明:在Activity的context(上下文环境)之外调用startActivity()方法时
需要给Intent设置一个flag:FLAG_ACTIVITY_NEW_TASK
所以在BroadcastReceiver的onReceive()方法中启动Activity应写为:
Intent intent=new Intent(context,AnotherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
之前描述了问题的现象和解决办法,现在试着解释一下原因:
1 在普通情况下,必须要有前一个Activity的Context,才能启动后一个Activity
2 但是在BroadcastReceiver里面是没有Activity的Context的
3 对于startActivity()方法,源码中有这么一段描述:
Note that if this method is being called from outside of an
{@link android.app.Activity} Context, then the Intent must include
the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because,
without being started from an existing Activity, there is no existing
task in which to place the new activity and thus it needs to be placed
in its own separate task.
说白了就是如果不加这个flag就没有一个Task来存放新启动的Activity.
4 其实该flag和设置Activity的LaunchMode为SingleTask的效果是一样的
如果在BroadcastReceiver的onReceive()方法中如下启动一个Activity
Intent intent=new Intent(context,AnotherActivity.class);
context.startActivity(intent);
可捕获异常信息:
android.util.AndroidRuntimeException:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
Is this really what you want?
它说明:在Activity的context(上下文环境)之外调用startActivity()方法时
需要给Intent设置一个flag:FLAG_ACTIVITY_NEW_TASK
所以在BroadcastReceiver的onReceive()方法中启动Activity应写为:
Intent intent=new Intent(context,AnotherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
之前描述了问题的现象和解决办法,现在试着解释一下原因:
1 在普通情况下,必须要有前一个Activity的Context,才能启动后一个Activity
2 但是在BroadcastReceiver里面是没有Activity的Context的
3 对于startActivity()方法,源码中有这么一段描述:
Note that if this method is being called from outside of an
{@link android.app.Activity} Context, then the Intent must include
the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because,
without being started from an existing Activity, there is no existing
task in which to place the new activity and thus it needs to be placed
in its own separate task.
说白了就是如果不加这个flag就没有一个Task来存放新启动的Activity.
4 其实该flag和设置Activity的LaunchMode为SingleTask的效果是一样的
1 0
- Android初学习 - 在BroadcastReceiver中启动Activity的问题
- 在BroadcastReceiver中启动Activity的问题
- 在BroadcastReceiver中启动Activity的问题
- 在BroadcastReceiver中启动Activity的问题
- 关于在BroadcastReceiver中启动Activity的问题
- Android BroadcastReceiver启动Activity中遇到的问题
- 在BroadcastReceiver中启动Activity
- 在BroadcastReceiver中启动Activity:
- Android -- BroadcastReceiver 中启动Activity
- 在BroadcastReceiver中启动Service或Activity
- 在BroadcastReceiver中启动Service或Activity
- android 从broadcastReceiver的onReceive方法中启动activity
- BroadcastReceiver中启动Activity
- BroadcastReceiver中启动Activity
- Android初学习 - 在Service里面启动Activity
- Android 如何在service和BroadCastReceiver中 启动activity和alertdialog?
- 从BroadcastReceiver中启动activity
- 在service和BroadCastReceiver中 启动activity和alertdialog
- WebKit内核学习之二
- linux命令学习记录
- Notification跳转后回退不会到桌面
- dubbo源码编译
- 历经流年,风轻云淡
- Android初学习 - 在BroadcastReceiver中启动Activity的问题
- 云服务器如何连接LoRa网关
- Unity3D核心类型一览
- 梦回桃花源/孑影吟风
- 溯源之梦回秦淮
- source insight打不开
- 北美IT公司大致分档
- 吴晓波领衔“大头频道”音频版独家入驻喜马拉雅FM意欲何为?
- 设置mysql允许外部IP访问