HandlerThread类和IntentService类的基本原理
来源:互联网 发布:联通和电信光纤端口 编辑:程序博客网 时间:2024/05/14 11:33
需求:有时需要从主线程往工作线程发送消息,在工作线程中处理消息。
原理:
HandlerThread类
HandlerThread提供一个getLooper的方法,返回一个looper对象。创建handler实例,handler=new Handler(looper),这样handler就与此looper对象关联起来,然后looper轮询工作线程的MessageQueue,处理消息。
IntentService类
我们知道启动service的时候是StartService(intent),会将本次启动的一个intent对象传过来,结合HandlerThread的特点,可以设计在工作线程也就是HandlerThread实例中处理intent。结合下面的IntentService简化代码实例,我们发现onStartCommand方法在各种业务中几乎都是一样的,onDestroy方法也是固定模式,onCreate方法中也只有接到消息内容intent的时候处理方法不一样,所以把这个处理intent的方法剥离出来,其他部分代码固定下来,就形成了Intentservice类,其子类只需要实现剥离处理的那个抽象类实现处理方法即可。
总的来说就是,将HandlerThread与IntentService结合起来使用,可以从主线程多次启动intentservice并传送intent,启动后IntentService会创建一个HandlerThread线程实例,将handler和HandlerThread关联,handler将intent包装消息发出去,然后handler处理消息,处理完毕停止本次启动。
启动模式的Service的三个主要方法:
public void onCreate() {}
public int onStartCommand(Intent intent, int flags, int startId) {}
public void onDestroy() {}
应用:比如应用于为音乐下载提供服务,每点击下载音乐一次,就会将上述的过程执行一遍。
HandlerThread简化代码实例:
public class MyHandlerThread extends Thread {private Looper looper;public Looper getLooper(){if(!isAlive())looper=null;if(looper==null){synchronized (this) {try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return looper;}@Overridepublic void run() {//创建消息队列Looper.prepare();//获取looper对象looper=Looper.myLooper();synchronized (this) {this.notify();}//开始轮询Looper.loop();}public void quit(){if(looper!=null){looper.quit();}}
public abstract class MyIntentService extends Service {private HandlerThread thread;private Handler handler;@Overridepublic void onCreate() {thread=new HandlerThread("workthread");thread.start();Looper looper=thread.getLooper();handler=new Handler(looper){@Overridepublic void handleMessage(Message msg) {//处理异步任务handlerIntent((Intent)msg.obj);//停止本次启动stopSelf(msg.arg1);}};super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Message msg=Message.obtain();msg.arg1=startId;msg.obj=intent;handler.sendMessage(msg);return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {thread.quit();super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}public abstract void handlerIntent(Intent intent) ;
- HandlerThread类和IntentService类的基本原理
- Android中IntentService和HandlerThread的分析
- 基于 HandlerThread 的 IntentService
- Android中的HandlerThread和IntentService
- 关于intentservice和handlerthread的一点小理解
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- HandlerThread、IntentService
- Android开发之IntentService和HandlerThread分析
- Android中HandlerThread和IntentService详解
- IntentService源码分析以及HandlerThread的用法
- HandlerThread类的使用和分析
- HandlerThread 类的使用
- Android HandlerThread和IntentService用法和源码解析
- Android中线程形态AsyncTask、HandlerThread 和 IntentService简介
- AsyncTask,HandlerThread,IntentService 与 ThreadPool 分别适合的使用场景
- 关于HandlerThread类的学习心得
- 扩展IntentService类的使用
- LC振荡电路
- RedHat 6 配置网卡team
- 黑马程序员——IO流-字节流
- 14、SQL Server 使用子查询和CTE提供数据之相关子查询
- linux和windows下用setsockopt设置send(), recv() 超时的参数的一点区别
- HandlerThread类和IntentService类的基本原理
- Eclipse各种图标的含义
- 15、SQL Server 使用子查询和CTE提供数据之关系除
- 内存映射函数mmap的初步理解
- Reclaimable Storage -- 可回收表空间
- 黑马程序员——IO流-字符流&装饰设计模式
- HDU-2048神、上帝以及老天爷
- oracle学习之存储过程(1)语法
- git设置命令别名