当前位置: 首页 > news >正文

安卓广播使用时的ANR问题,onReceive的生命周期为10秒

在 Android中,程序的响应(Responsive)被活动管理器(Activity Manager)和窗口管理器(Window Manager)这两个系统服务所监视,当BroadcastReceiver在10秒内没有执行完毕,Android会认为该程序无响应,所以在 BroadcastReceiver里不能做一些比较耗时的操作,否则会弹出ANR(Application No Response)的对话框。如果需要完成一项比较耗时的工作,应该通过发送Intent给Service,由Service来完成,而不是使用子线程的方法来解决,因为BroadcastReceiver的生命周期很短(在onReceive()执行后BroadcastReceiver的实例就会被销毁),子线程可能还没有结束BroadcastReceiver就先结束了。如果BroadcastReceiver结束了,它的宿主进程还在运行,那么 子线程还会继续执行。但宿主进程此时很容易在系统需要内在时被优先杀死。因为它属于空进程(没有任何活动组件的进程)。

每次广播消息到来时,都会创建BroadcastReceiver实例来执行onReceive()方法。


广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用 Context.sendBroadcast()、Context.sendOrdeedBroadcast()、 context.sendStickyBroadcast()来实现的,通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,这 个特性跟JMS中的Topic消息接收者类似,要实现一个广播接收者方法如下:

onReceive的生命周期为10秒,所以里面的操作不能超过10秒

 

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. This method is always called within the main thread of its process, unless you explicitly asked for it to be scheduled on a different thread usingregisterReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler). When it runs on the main thread 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().

http://www.xdnf.cn/news/812323.html

相关文章:

  • windows安装npm教程
  • power supply是如何上报电池信息的
  • 信息学奥赛需要储备的数学知识有哪些?
  • 计算机专业都要学哪些课程?(非常详细)零基础入门到精通,收藏这一篇就够了(1)
  • Alpha-Blending 技术简介
  • 分享136个PHP源码,总有一款适合您
  • hh.exe的另类用法
  • 谈谈新库requests-html以及python爬虫 requests
  • 多路径 I/O
  • extensionloaded php,PHP函数extension_loaded()用法实例,extensionloaded_PHP教程
  • 磁盘分区全解:快速搞定硬盘分区
  • 2024年,值得收藏!推荐一些好用的数据库管理工具合集!
  • 2023年程序员必须知道的10大代码网站!
  • Red Hat Linux iso镜像文件(3)个
  • MATLAB—取整函数汇总(floor、round、ceil、fix)
  • 《软件安装与使用教程》— 最新Acrobat 8专业版Windows安装详细教程
  • EL表达式知识
  • 干货!12个程序员证书​,含金量超高
  • abab 模型进展|MiniMax 发布万亿 MoE 模型abab 6.5
  • ArrayAdapter 支持单选
  • AutoIT的下载与安装
  • 推荐开源项目:IconChanger - 简单易用的App图标更换神器
  • SAP RFC介绍:关于sRFC,aRFC,tRFC,qRFC和bgRFC
  • 截屏闪光
  • 性能测试 Performance Test
  • 分享125个ASP源码,总有一款适合你
  • SQLServer简繁互换
  • impex 语法
  • 什么是中台?一篇带你了解中台的概念
  • 无线网络通信技术完全介绍