Android线程和线程池(三)--IntentService
来源:互联网 发布:反马赛克软件torch 编辑:程序博客网 时间:2024/05/17 23:25
IntentService
IntentService是一种特殊的Service,他继承了Service,并能够处理异步请求,是一个抽象类,因此在我们使用的时候必需创建它的子类才能够使用IntentService。IntentService可用于执行后台耗时的任务,当任务执行完成之后会自动停止,同时由于IntentService是服务的原因,和Activity是同级的,因此他的优先级比线程要高很多,因此IntentService比较适合执行一些高优先级的后台任务,因为优先级高,所以不容易被系统杀死。在实现上,IntentService封装了HandlerThread和Handler。
启动IntentService的方式和启动传统的Service一样,每一个耗时操作会以工作队列的方式在IntentService的onHandlerIntent回调方法中执行,并且每次只会执行一个工作线程,执行完第一个再执行第二个。因此如果需要执行后台耗时操作,就省去了我们在Service中手动开线程的麻烦,而且当操作完成后,我们不用手动停止Service。
@Override public void onCreate() { // TODO: It would be nice to have an option to hold a partial wakelock // during processing, and to have a static startService(Context, Intent) // method that would launch the service & hand off a wakelock. super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
这段代码为IntentService源码中的onCreate方法,当IntentService第一次启动时,会调用onCreate方法,我们可以看到,在onCreate方法中创建了一个HandlerThread,并且用它的Looper来构造了一个Handler对象,这样通过mServiceHandler发送的消息就会在HandlerThread中执行。
IntentService在启动的时候会调用onStartCommand方法,又因为IntentService可以多次被启动,因此每次启动的时候都会调用onStartCommand方法,该方法会调用onStart方法:
@Override public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } @Override public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); }
我们可以看到,在IntentService启动的时候,只是通过mServiceHandler发送了一个消息,该消息包括启动的Intent信息和启动的ID,这个消息会在HandlerThread的方法handleMessage中被处理,而处理仅仅是再次将其转交给onHandleIntent方法,该方法是我们需要具体实现的方法,在处理完成后会调用stopSelf方法来结束。因此如果我们后台有多个任务,即多次启动IntentService,那么消息就会被放入HandlerThread的消息队列中,最终在onHandlerIntent中被处理,当所有任务处理完成之后才会调用stopSelf方法。
下面我们用一个示例演示IntentService的使用:
public class MyIntentService extends IntentService{ public static final String ACTION_FIRSTTASK = "com.zy.action.FIRST_TASK"; public static final String ACTION_SECONDTASK = "com.zy.action.SECOND_TASK"; public static final String ACTION_THIRDTASK = "com.zy.action.THIRD_TASK"; public MyIntentService(){ super("task_action"); } @Override public void onCreate() { super.onCreate(); } @Override protected void onHandleIntent(Intent intent) { //String action = intent.getStringExtra("task_action"); String action = intent.getAction(); if (action.equals(ACTION_FIRSTTASK)){ //do something System.out.println("Service started."); }else if (action.equals(ACTION_SECONDTASK)){ //do something }else if (action.equals(ACTION_THIRDTASK)){ //do something } System.out.println(action); //模拟耗时操作 SystemClock.sleep(3000); } @Override public void onDestroy() { System.out.println("Service destroyed."); super.onDestroy(); }}
注意我们必须要重写一个无参的构造函数,如上代码所示。
使用代码如下:
Intent startServiceIntent = new Intent(this,MyIntentService.class); startServiceIntent.setAction(MyIntentService.ACTION_FIRSTTASK); startService(startServiceIntent); startServiceIntent.setAction(MyIntentService.ACTION_SECONDTASK); startService(startServiceIntent); startServiceIntent.setAction(MyIntentService.ACTION_THIRDTASK); startService(startServiceIntent);
如上我们启动了三次我们定义的IntentService,观察打印结果发现任务按照顺序执行,且所有任务按顺序执行,所有任务执行完毕之后调用onDestroy方法。
如下所示为执行结果:
02-23 21:51:27.858 2338-2338/com.example.zhangyi.dzbg I/System.out﹕ Service created.02-23 21:51:27.859 2338-2352/com.example.zhangyi.dzbg I/System.out﹕ Service started.02-23 21:51:27.859 2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.FIRST_TASK02-23 21:51:30.860 2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.SECOND_TASK02-23 21:51:33.860 2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.THIRD_TASK02-23 21:51:36.862 2338-2338/com.example.zhangyi.dzbg I/System.out﹕ Service destroyed.
注意:因为IntentService也是服务,因此需要我们在AndroidManifest.xml中进行注册才能使用。
- Android线程和线程池(三)--IntentService
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- Android 的线程(AsyncTask、IntentService详解)和线程池
- Android 的线程(AsyncTask、HandlerThread、IntentService详解)和线程池
- 线程使用教程 三 IntentService
- android 线程(AsyncTask,Threadhandler,intentService)详解
- Android线程IntentService源码分析
- Android中线程形态AsyncTask、HandlerThread 和 IntentService简介
- android intentService(三)
- Android线程和线程池(四)--Android线程池
- AsyncTask,IntentService工作原理分析&Android线程池
- Service IntentService Android中的线程 MediaPlayer
- Android 中的线程形态 -- AsyncTask,HandlerThread,IntentService
- Android Service跟线程及IntentService使用
- Android知识点之多线程:Thread、AsyncTask、IntentService
- Android 线程和线程池
- Android线程和线程池
- Android 线程和线程池
- Android日常开发60条经验
- JavaMail
- 高性能网站架构设计之缓存篇(5)- Redis 集群(上)
- VBA outlook实战#02--自动分类邮件
- iOS小明开发笔记(二十五) (NSIndexPath)
- Android线程和线程池(三)--IntentService
- HNOI2007.BZOJ1189.紧急疏散(最大流 && 二分)
- C#-StructLayoutAttribute(结构体布局)
- Volley源码解析<六> HttpStack网络请求
- Caffe: solver及其配置
- 高性能网站架构设计之缓存篇(4)- Redis 主从复制
- CUDA学习笔记--上下文
- Android进程优先级详解
- 业务知识