Android Service之串行化Service:IntentService(系列3)
来源:互联网 发布:keeper是什么软件 编辑:程序博客网 时间:2024/04/20 10:02
转载请注明出处:http://blog.csdn.net/zhangphil
Android Service之串行化Service:IntentService(系列3)
之前写了一系列的文章关于Android Service。
(文章1)《Android Service简介(系列1)》文章链接地址:http://blog.csdn.net/zhangphil/article/details/49373939
(文章2)《Android Activity与Service数据交互:Binder、bindService(系列2)》文章链接地址:http://blog.csdn.net/zhangphil/article/details/49385005
文章1简介了如何使用Android Service。文章2简介了出于前台的Android Activity与后台的Service数据交互。
Android最基础的类Service,提供了通常所需的后台任务处理框架。上层处于前台的Activity通过startService启动后台的Service,然后Service进入onStartCommand做耗时任务。这里面潜在有一个问题,假设Activity里面如果不停的startService启动后台的Service,那么将导致onStartCommand不断的反复调用。这意味着,假如代码中这个Service里面的onStartCommand代码块涉及到并发、多线程时候,就要非常小心处理这种多任务情况。可事实上这种多线程任务在Service里面编程设计实现起来比较复杂和琐碎,好在Android体系架构中为了处理这种开发场景,提供了IntentService。
IntentService也是一个Android Service,IntentService在类层次结构上继承自Service。但IntentService与Service有很大不同。
IntentService是串行化处理后台的Service任务的。Service可以在onStartCommand设计复杂的多线程编程模型。然而IntentService已经将此onStartCommand重载,并引入onHandleIntent,开发者不用关心onStartCommand的任务传导给onHandleIntent的内部实现机制。开发者只需要专心在onHandleIntent里面写串行化的任务逻辑代码即可。开发者该怎么简单的理解IntentService呢?可以这么简单的认为IntentService的编程模型:
(1)当上层代码(通常也就是前台的Activity通过startService启动IntentService)启动IntentService后,IntentService将进入onHandleIntent(不用关心onStartCommand是如何维护任务队列和派发任务的),由于Android系统设计时候已经将onHandleIntent线程化处理,所以可以随意的在onHandleIntent里面做大量耗时操作。
(2)要注意,虽然可以通过startService反复启动IntentService,但是在IntentService中,这些任务将排成一个FIFO的任务队列,依次顺序执行,而不是并发同时执行这些任务。可以startService Intent A,B,C,D,E,,,,但只会串行的顺序执行A,B,C,D,E,,,,。只有在前一个任务完成后才会接着顺序执行下一个任务。换一种理解方式,可以认为同时为每个startService运行了多个线程onHandleIntent,但这些线程只会是FIFO顺序执行。这不同于Service,如果是Service,那么将在onStartCommand并发执行。
(3)虽然可以在上层Activity中反复多次执行startService启动IntentService从而进入onHandleIntent执行后台任务,但只需要调用一次stopService,就可以停止所有IntentService队列中的onHandleIntent,但有一点:如果在调用stopService时候,其中有一个onHandleIntent正在处理还尚未完成,那么将此onHandleIntent不受stopService影响直到处理完成。
给出测试代码。
IntentService的类:
测试的Activity MainActivity.Java:
MainActivity.java需要的布局文件activity_main.xml文件提供两个按钮,一个用于启动IntentService的start;另外一个是关闭IntentService的stop:
- 顶
- 0
- 踩
- Android Service之串行化Service:IntentService(系列3)
- Android Service之串行化Service:IntentService(系列3)
- Android Service之串行化Service:IntentService(系列3)
- Android Service之串行化Service:IntentService(系列3)
- Android Service串行服务:IntentService(系列3)
- 【Android学习】Android服务之Service(3)--IntentService
- Android:Service 之 IntentService(上)
- Android之Service(六):使用IntentService
- android service 之二(IntentService)
- android service 之二(IntentService)
- android之Service(2)IntentService
- Android---43---Service之IntentService
- android中service之intentService
- android service 之二(IntentService)
- android service 之二(IntentService)
- Android之IntentService和Service
- Android Service完全解析(Service,IntentService)
- Android-服务Service(3)-IntentService
- 优秀Python学习资源收集汇总(强烈推荐)
- Apache与Nginx比较
- mysql取随机范围内地随机数
- Photo OCR
- SuperMap iObject常见问题解答集锦(九)
- Android Service之串行化Service:IntentService(系列3)
- iframe页面操作easyui控件
- 牛顿法(newton's method)求解函数极值
- tomcat虚拟路径配置
- 剑指offer-求1+2+3+…+n
- .DS_Store 文件怎么彻底删除?
- iOS面向切面编程
- Leetcode 328. Odd Even Linked List (Medium) (cpp)
- 如何释放含有NSTimer的UITableViewCell(SubView)