在BroadcastReceiver中接收到广播之后,有时会进行一些耗时的操作。大家一般有两种写法:
1)在当前BroadcastReceiver中另起线程操作
2)由当前BroadcastReceiver启动新的Service,在新的Service中操作
其实,第一种方法并不推荐。因为大家都知道,安卓在内存不足或其他资源不够的情况下会作清理。而BroadcastReceiver在onReceive()调用后,就只剩下一个线程在跑了,没有service的级别高!
在http://developer.android.com/guide/components/processes-and-threads.html中有一段
Because a process running a service is ranked higher than a process with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply create a worker thread—particularly if the operation will likely outlast the activity. For example, an activity that's uploading a picture to a web site should start a service to perform the upload so that the upload can continue in the background even if the user leaves the activity. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. This is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.
所以,推荐大家用第2种写法。
相关推荐
BroadCastReceiver
对于这样的BroadcastReceiver,通常是在产生某个特定的系统事件之后,进行相应的操作,比如说wifi网卡打开时,给用户一个提示;ii. 第二种注册方法相对第一种要灵活的多,这样注册的BroadcastReceiver通常用于更新UI...
android的broadcastReceiver的介绍。
BroadcastReceiver,两种注册方式
Android \BroadCastReceiver 最全的简介
BroadcastReceiver学习(01)
android BroadcastReceiver相关测试代码。我测试所用的。
介绍BroadcastReceiver使用的demo,具体的使用方法可以参看我的博客
BroadcastReceiver通过接口回调更新界面,BroadcastReceiver不写在Activity里面,便于代码的封装,更加便于阅读
动态注册broadcastreceiver短信接受
android BroadcastReceiver 源码 学习
以获取电池电量为例对BroadcastReceiver和接口进行了简单讲解
BroadcastReceiver应用详解 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播。
这是一个关于Android中四大组件之一 —— BroadcastReceiver,即广播的一个demo,里面涉及到BroadcastReceiver的两种注册方式,也简单的实现了广播,通过点击按钮发送广播,收到广播后打印日志。
Android学习之BroadcastReceiver总结
Android下BroadcastReceiver的简单示例。详见博客:http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html
本集中提到的BroadcastReceiver就是此应用的典范。Android通过广播机制,能够让订阅者接受到想听到的广播,并进行进一步的处理和操作。如果你想在虚拟的android世界中创建属于你自己的电台广播,那么就赶紧点击本集...
Android-BroadcastReceiver高级应用,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SystemBroadcastReceriver.html