BroadcastReceiver类onReceive方法求解.

phphis 2011-11-08 03:17:33
因为我要再BroadcastReceiver里面启动Activity,所以就上网搜索了下相关的信息.
有很多人讨论的时候都说最好不要这样,还给出了API帮助文档里面的说明.
目前我使用是没问题的,但是如果官方说不要这样,那肯定是有问题的,于是我拿起帮助文档通过和google翻译一起努力,查看了下面的这段说明:

This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values. The function is normally called within the main thread of its process, so you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed). You cannot launch a popup dialog in your implementation of onReceive().

我的理解是这样的:

onReceive()是当BroadcastReceiver收到Intent广播后调用的回调函数.
在此期间,你可以使用BroadcastReceiver的其它方法查看/修改Intent的数据.
这个函数运行在其进程的主线程,所以你不应该让它执行耗时的操作.
(如果一个进程的主线程被阻塞10秒,那么系统会提示用户kill掉此进程)(我试了新开线程也只是允许20秒阻塞)
不要在onReceiver()里面启动一个弹出对话框.

疑问就是,如果onReceive()的操作不造成阻塞,那么就没问题了不是吗?
但是最后为什么还要说:不要在onReceiver()里面启动一个弹出对话框.
...全文
1332 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
BroadcastReceive的生命周期很短,onReceive方法结束之后,生命周期就结束了,此时dialog没有dismiss的话,会导致一些问题的。。
  • 打赏
  • 举报
回复
倒!
很彻底!
lyltiger 2011-11-16
  • 打赏
  • 举报
回复
也是耗时的

  • 打赏
  • 举报
回复
那这个问题如何解决呢??

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧