什么是IntentService? (本文转自http://blog.csdn.net/gaojie314/archive/2010/11/28/6040701.aspx

官方的解释是:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through android.content.Context.startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

意思是说:IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对象,在适当的时候会停止自己(一般在工作完成的时候). 所有的请求的处理都在一个工作线程中完成,它们会交替执行(但不会阻塞主线程的执行),一次只能执行一个请求.(**本人修改了原文的一些翻译)

 
下面是要分析的源码:
public abstract class IntentService extends Service {
        private volatile Looper mServiceLooper;         private volatile ServiceHandler mServiceHandler;
        private String mName;         private boolean mRedelivery;    
        private final class ServiceHandler extends Handler {
                public ServiceHandler(Looper looper) {                         super(looper);                 }                     @Override                 public void handleMessage(Message msg) {                         onHandleIntent((Intent)msg.obj);                         stopSelf(msg.arg1);                 }
        }

从源码可以分析出: IntentService 实际上是Looper,Handler,Service 的集合体,他不仅有服务的功能,还有处理和循环消息的功能.
下面是onCreate()的源码:

        @Override         public void onCreate() {                 super.onCreate();
                HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");                 thread.start();
                mServiceLooper = thread.getLooper();                 mServiceHandler = new ServiceHandler(mServiceLooper);         }

分析:IntentService创建时就会创建Handler线程(HandlerThread)并且启动,然后再得到当前线程的Looper对象来初始化IntentService的mServiceLooper,接着创建mServicehandler对象.

 
下面是onStart()的源码:
        @Override         public void onStart(Intent intent, int startId) {                 Message msg = mServiceHandler.obtainMessage();                 msg.arg1 = startId;                 msg.obj = intent;
                mServiceHandler.sendMessage(msg);         }

分析:当你启动IntentService的时候,就会产生一条附带startId和Intent的Message并发送到MessageQueue中,接下来Looper发现MessageQueue中有Message的时候,就会停止Handler处理消息,接下来处理的代码如下:

        @Override         public void handleMessage(Message msg) {                         onHandleIntent((Intent)msg.obj);                         stopSelf(msg.arg1);         }

接着调用 onHandleIntent((Intent)msg.obj),这是一个抽象的方法,其实就是我们要重写实现的方法,我们可以在这个方法里面处理我们的工作.当任务完成时就会调用stopSelf(msg.arg1)这个方法来结束指定的工作.

当所有的工作执行完后:就会执行onDestroy方法,源码如下:

        @Override         public void onDestroy() {                 mServiceLooper.quit();         }

服务结束后调用这个方法 mServiceLooper.quit()使looper停下来.
通过对源码的分析得出:     这是一个基于消息的服务,每次启动该服务并不是马上处理你的工作,而是首先会创建对应的Looper,Handler并且在MessageQueue中添加的附带客户Intent的Message对象,当Looper发现有Message的时候接着得到Intent对象通过在onHandleIntent((Intent)msg.obj)中调用你的处理程序.处理完后即会停止自己的服务.意思是Intent的生命周期跟你的处理的任务是一致的.所以这个类用下载任务中非常好,下载任务结束后服务自身就会结束退出

Android Service学习之IntentService 深入分析的更多相关文章

  1. 【转】android service 之二(IntentService)

    原文网址:http://rainbow702.iteye.com/blog/1143286 不管是何种Service,它默认都是在应用程序的主线程(亦即UI线程)中运行的.所以,如果你的Service ...

  2. android Service 学习总结

    学习android开发已经四五个月,由于项目中职责的原因一直没有接触过Service的实际项目,今天重新学一遍Service用法. 问题: 作为四大组件,为什么需要Service? 它与Thread又 ...

  3. Android Service学习之AIDL, Parcelable和远程服务

    AIDL的作用     由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象.在Android平台,一个进程通常不能访问另一个进程的内存空 ...

  4. Android Service学习之本地服务

    Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...

  5. Android Service学习

    Android 中的 Service 全面总结 引用别人的博客:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 好文章 1.S ...

  6. android service 学习

    参考:http://www.cnblogs.com/allin/archive/2010/05/15/1736458.html http://www.cnblogs.com/allin/archive ...

  7. Android Service用法知识点的讲解

    Android Service 学习Service相关知识点: android service 的基础知识,生命周期,service分类,运行地点(本地服务,远程服务),运行类型(前台服务,后台服务) ...

  8. Android Service总结03 之被启动的服务 -- Started Service

    Android Service总结03 之被启动的服务 -- Started Service 版本 版本说明 发布时间 发布人 V1.0 添加了Service的介绍和示例 2013-03-17 Sky ...

  9. Android WiFiDirect 学习(二)——Service Discovery

    Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...

随机推荐

  1. fragment类onresume里面刷新操作处理

    今天项目中涉及fragment中嵌套多个fragment,但是要根据tag去展示对应的fragment,而不是默认展示的第一个fragment,如果使用activity很容易想到onpause(),o ...

  2. children

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> ...

  3. JUnit test case 执行顺序

    转自:JUnit中按照顺序执行测试方式 很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前 ...

  4. jQuery 截取double数据 重新赋值

    $('.prioritySort').each(function(i){ $(this).text($(this).text().substring(0,$(this).text().indexOf( ...

  5. json格式数据 ,将数据库中查询的结果转换为json(方式2)

    controller: /*** * 返回所有版本的信息,json的形式返回到前台 * @return */ @RequestMapping(value="/getAllVersion&qu ...

  6. KindeEditor图片上传插件用法

    因业务需要找了款插件 KindeEditor编辑器确认挺好用,但无奈技术有限,上传配置不知,故问度娘! 图片上传对于部分新手来说有时候是一件非常头疼的事,今天来分享一下项目中使用到的这个插件Kinde ...

  7. Zmodem协议

    Zmodem文件传输协议 做zeppelin测试时,自己安装了虚拟机,发现一个在linux和windows之间特别方便的命令行rz/sz工具. Install # 由于虚拟机不能上网,所以先挂载镜像. ...

  8. 对一个表中所有列数据模糊查询adoquery

    如何用adoquery对一个表中所有列进行模糊查询: procedure TForm3.Button4Click(Sender: TObject); var ASql,AKey: string; I: ...

  9. yii框架中关于控制器中filter过滤器和外部action的使用

    在yii框架中,控制器的过滤器分为执行前和执行后,这里举例是在执行控制器前的过滤. 需要在components/文件夹下定义公共的TestAction.php文件,并且实现run()方法.这个acti ...

  10. Matlab - 矩阵元素引用

    >> A = [ ; ; ] A = 1. 选择第m行n列的元素 >> A(,) ans = 2. 选择第i列所有元素 >> A(:,) ans = 3. 选择第j ...