service 默认也运行在 UI 线程,所以里面不能直接做耗时操作,要做耗时操作还得开启子线程来做。

IntentService 就是一个 Service, 只不过里面给你默认开启了一个子线程来处理所有的 intent 请求。

而多次调用 startService 时所有请求都会放到这个子线程中一个接一个的按顺序执行,等所有的请求都处理完毕后 service 会自动销毁。

一句话描述就是用一个子线程来依次处理各个请求,请求按顺序一个接一个执行。按顺序执行是通过 Looper 队列实现的。

看看源码就清晰了

/**
* 这段注释应该多读几遍
* IntentService is a base class for Service that handle asynchronous
* requests (expressed as Intent) on demand. Clients send requests
* through 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.
*
* 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.
*/
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) {
// handleMessage 还是在 onCreate 中启动的子线程中执行,因为 Looper 在哪个线程 handler 就在哪个线程执行
// 消息队列保证了所有请求按顺序执行
onHandleIntent((Intent)msg.obj); // 这里貌似停止了服务, 其实不是只有当 startId 等于最后一次启动服务时的 startId 时服务才会销毁
stopSelf(msg.arg1);
}
} public IntentService(String name) {
super();
mName = name;
} @Override
public void onCreate() {
super.onCreate();
// onCreate 时启动一个带有 Looper 的子线程
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start(); mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
} @Override
public void onStart(Intent intent, int startId) {
// 每次 onStart 时都发送一个消息到消息队列中等待执行
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
} @Override
public void onDestroy() {
mServiceLooper.quit();
} @Override
public IBinder onBind(Intent intent) {
return null;
} /**
* 这段注释也应该多读几遍
* This method is invoked on the worker thread with a request to process.
* Only one Intent is processed at a time, but the processing happens on a
* worker thread that runs independently from other application logic.
* So, if this code takes a long time, it will hold up other requests to
* the same IntentService, but it will not hold up anything else.
* When all requests have been handled, the IntentService stops itself,
* so you should not call stopSelf.
*/
@WorkerThread
protected abstract void onHandleIntent(Intent intent);
}

看似简单其实里面东西挺多,发现网上好多人理解有误区,有人认为是每 start 一次都开启一个 worker 线程, 其实不是如果 service 还没有销毁多次调用

都只有一个线程来按顺序来处理请求。

什么是 IntentService的更多相关文章

  1. 什么时候用IntentService

    IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...

  2. IntentService

    http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...

  3. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  4. HandlerThread和IntentService

    HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...

  5. android 中IntentService的作用及使用

    IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...

  6. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  7. Android中Services之异步IntentService

    IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1)  它创 ...

  8. IntentService源码分析

    和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...

  9. Android Service 与 IntentService

    Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行

  10. Android IntentService完全解析 当Service遇到Handler

    一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...

随机推荐

  1. JSONKit 简单使用

    http://blog.csdn.net/l_ch_g/article/details/8477187 例子上写的比较浅显易懂, 不过我还是稍微总结一下: 导入JSONKit.h之后 字符串转NSDi ...

  2. 华为2013校招之哈工大威海 上机试题之一:报数问题:设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去直 到所有的人都出圈为止。现要打印出出圈次序。

    1.  报数游戏 问题描述: 设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去 ...

  3. 【OpenStack】OpenStack系列10之Horizon详解

    一.参考其他资料即可.可以采用haproxy+apache+horizon方式部署,haproxy/httpd支持ssl.

  4. TCP/IP详解学习笔记(5)-- ICMP:internet 控制报文协议

    1.概述      ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制 ...

  5. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  6. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

  7. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  8. ORACLE清除某一字段重复的数据(选取重复数据中另一个字段时期最大值)

    需求:资产维修表中同一资产可能维修完继续申请维修,这时候维修状态需要根据最近的维修时间去判断维修状态,所以同一资产ID下会出现重复的数据(维修审批通过,维修审批未通过),或者可能不出现(未申请维修), ...

  9. July 30th, Week 31st Saturday, 2016

    No matter how far you may fly, never forget where you come from. 无论你能飞多远,都别忘了你来自何方. No matter how fa ...

  10. Android中几种定位 方式

    介绍的几种定位方式 http://www.cnblogs.com/cuihongyu3503319/p/3863867.html 百度地图api: http://lbsyun.baidu.com/in ...