Android多线程:IntentService用法&源码分析
来源:互联网 发布:淘宝卖什么快 编辑:程序博客网 时间:2024/06/06 05:29
前言
多线程的应用在Android开发中是非常常见的,常用方法主要有:
- 继承Thread类
- 实现Runnable接口
- AsyncTask
- Handler
- HandlerThread
- IntentService
今天,我将全面解析多线程其中一种常见用法:IntentService
目录
1. 定义
IntentService是Android里面的一个封装类,继承自四大组件之一的Service。
2. 作用
处理异步请求,实现多线程
3. 工作流程
注意:若启动IntentService 多次,那么每个耗时操作则以队列的方式在 IntentService的onHandleIntent回调方法中依次执行,执行完自动结束。
4. 实现步骤
- 步骤1:定义IntentService的子类:传入线程名称、复写onHandleIntent()方法
- 步骤2:在Manifest.xml中注册服务
- 步骤3:在Activity中开启Service服务
5. 具体实例
- 步骤1:定义IntentService的子类:传入线程名称、复写onHandleIntent()方法
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 步骤2:在Manifest.xml中注册服务
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 步骤3:在Activity中开启Service服务
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 结果
6. 源码分析
接下来,我们会通过源码分析解决以下问题:
- IntentService如何单独开启一个新的工作线程;
- IntentService如何通过onStartCommand()传递给服务intent被依次插入到工作队列中
问题1:IntentService如何单独开启一个新的工作线程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
问题2:IntentService如何通过onStartCommand()传递给服务intent被依次插入到工作队列中
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 总结
从上面源码可以看出,IntentService本质是采用Handler & HandlerThread方式:- 通过HandlerThread单独开启一个名为IntentService的线程
- 创建一个名叫ServiceHandler的内部Handler
- 把内部Handler与HandlerThread所对应的子线程进行绑定
- 通过onStartCommand()传递给服务intent,依次插入到工作队列中,并逐个发送给onHandleIntent()
- 通过onHandleIntent()来依次处理所有Intent请求对象所对应的任务
因此我们通过复写方法onHandleIntent(),再在里面根据Intent的不同进行不同的线程操作就可以了
注意事项1. 工作任务队列是顺序执行的。
如果一个任务正在IntentService中执行,此时你再发送一个新的任务请求,这个新的任务会一直等待直到前面一个任务执行完毕才开始执行
原因:
1. 由于onCreate() 方法只会调用一次,所以只会创建一个工作线程;
2. 当多次调用 startService(Intent) 时(onStartCommand也会调用多次)其实并不会创建新的工作线程,只是把消息加入消息队列中等待执行,所以,多次启动 IntentService 会按顺序执行事件
3. 如果服务停止,会清除消息队列中的消息,后续的事件得不到执行。
注意事项2:不建议通过 bindService() 启动 IntentService
原因:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
在IntentService中,onBind()是默认返回null的,而采用bindService() 启动 IntentService的生命周期是:onCreate() —>onBind()—>onunbind()—>onDestory()
并不会调用onstart()或者onstartcommand()方法,所以不会将消息发送到消息队列,那么onHandleIntent()将不会回调,即无法实现多线程的操作。
此时,你使用的是Service,而不是IntentService
7. 使用场景
线程任务需要按顺序、在后台执行的使用场景
最常见的场景:离线下载
由于所有的任务都在同一个Thread looper里面来做,所以不符合多个数据同时请求的场景。
8. 对比
8.1 IntentService与Service的区别
从属性 & 作用上来说
Service:依赖于应用程序的主线程(不是独立的进程 or 线程)
不建议在Service中编写耗时的逻辑和操作,否则会引起ANR;
IntentService:创建一个工作线程来处理多线程任务
此外:
1. IntentService为Service的onBingd()方式提供了默认实现:返回null
2. IntentService为Service的onStartCommand()方法提供了默认实现:将请求的intent添加到队列中
8.2 IntentService与其他线程的区别
- IntentService内部采用了HandlerThread实现,作用类似于后台线程;
- 与后台线程相比,IntentService是一种后台服务,优势是:优先级高(不容易被系统杀死),从而保证任务的执行
对于后台线程,若进程中没有活动的四大组件,则该线程的优先级非常低,容易被系统杀死,无法保证任务的执行
- Android多线程:IntentService用法&源码分析
- Android多线程:IntentService用法&源码分析
- android-----IntentService源码分析
- Android IntentService源码分析
- Android线程IntentService源码分析
- 【Android应用源码分析】IntentService 源码分析
- IntentService源码分析以及HandlerThread的用法
- Android IntentService浅谈以及源码分析
- Android开发:IntentService使用(源码分析)
- Android IntentService的使用和源码分析
- Android IntentService的使用和源码分析
- Android中IntentService源码分析及其使用
- Android面试题-IntentService源码分析
- Android之IntentService使用及源码分析
- IntentService源码分析
- IntentService源码分析
- IntentService源码分析
- IntentService源码分析
- java 模拟斗地主分派
- 行为型模式-中介者(mediator)
- rapidjson实现将得到的Value中的值变为string
- 欢迎使用CSDN-markdown编辑器
- JAVA中断线程
- Android多线程:IntentService用法&源码分析
- 认识J2SE
- 用py2exe打包运行后出现requests.exceptions.SSLError: [Errno 2]解决办法
- redis+lua实现防刷
- Variational Inferecne
- spring框架学习(三)junit单元测试
- CCF 201703-1 分蛋糕 JAVA实现
- 多线程(一) 多线程介绍及基本使用
- java.lang.NoClassDefFoundError: org/apache/commons/pool/ObjectPool