IntentService

IntentService

十一月 07, 2017

IntentService

1. IntentService是什么

1.1 特殊的Service

//继承了Service,可以执行高优先级的任务, 封装了HandlerThread和Handler

  • 本质上是一种特殊的Service, 继承自Service并且本身就是一个抽象类
  • 它的内部通过HandlerThread 和 Handler 实现异步操作

是继承并处理异步同步请求的一个类, 在IntentService内有一个工作线程来处理耗时操作, 启动IntentService的方式和启动传统的Service一样, 同时, 当任务执行完后, IntentService 会自动停止, 而不需要我们手动去控制stopSelf(), 另外, 可以启动IntentService多次, 而每一个耗时操作都会以工作队列的方式在IntentService的onHandlerIntent回调方法中执行, 并且 , 每次只会执行一个工作线程, 执行完第一个再执行第二个;(串行)

2. IntentService使用方法

2.1 必须实现两个方法

(创建IntentService时, 只需要实现onHandleIntent和构造方法, onHandleIntent为异步方法, 可以执行耗时操作)

写一个类继承IntentService

构造方法: 传入线程名称

1
2
3
4
public IntentService(String name) {
super();
mName = name;
}

onHandleIntent(): 进行Itent的耗时操作 后intent里携带信息, 并startService(intent),
最后MyIntentService.setUpdate(this);

1
2
@WorkerThread
protected abstract void onHandleIntent(@Nullable Intent intent)

3. IntentService源码解析

3.1 onCreate()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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 + "]");// 创建了HandlerThread来进行异步消息传递
thread.start();

mServiceLooper = thread.getLooper();// 传递的是HandlerThread的loop对象;
// (由于looper 对象和 HandlerThread绑定,而HandlerThread又是一个异步线程,把HandlerThread 持有的 looper 传递给 ServiceHandler
这样ServiceHandler就变成了处理异步线程的执行类)
mServiceHandler = new ServiceHandler(mServiceLooper);// 一个继承了Handler的Handler
}

intentService启动后还会调用onStartCommand();

3.2 onStartCommand():

1
2
3
4
5
6
7
8
9
10
11
/**
* You should not override this method for your IntentService. Instead,
* override {@link #onHandleIntent}, which the system calls when the IntentService
* receives a start request.
* @see android.app.Service#onStartCommand
*/
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart(intent, startId);//实际操作都在onStart
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}

3.4 onStart():

1
2
3
4
5
6
7
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}

3.5 ServiceHandler类:

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
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);// 如果没有参数会立即停止, 如果有参数会等待所有消息都处理完之后才会终止任务
}
}
```java
onHandleIntent: 是一个抽象方法, 在创建IntentService时一定要实现该方法(异步方法)
如果执行完会立刻销毁, 当有多个服务时会明确到执行完最后一个服务才会销毁;
stopSelf加参数;

**(Service里不能做耗时操作, 而IntentService可以执行耗时操作)**

```java
/**
* 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 {@link #stopSelf}.
*
* @param intent The value passed to {@link
* android.content.Context#startService(Intent)}.
* This may be null if the service is being restarted after
* its process has gone away; see
* {@link android.app.Service#onStartCommand}
* for details.
*/
@WorkerThread
protected abstract void onHandleIntent(@Nullable Intent intent);

本质上就是一个封装了HandlerThread 和 handler的异步框架

每次去实现时一定实现onHandleIntent()在里面进行耗时操作, 会按照顺序执行