IntentService是一种特殊的Service,它继承了Service并且是一个抽象类,因此必须创建它的子类才能使用IntentService。IntentService一般用于执行后台耗时任务,当任务执行完成会自动停止;同时由于它是一个服务,优先级要远远高于线程,更不容易被系统杀死,因此比较适合执行一些高优先级的后台任务。
一、IntentService使用方法
IntentService的使用方法比较简单,下面通过一个示例来说明IntentService的使用步骤。
1. 创建IntentService的子类,重写onHandleIntent方法,在onHandleIntent中执行耗时任务
public class ChildIntentService extends IntentService {
public ChildIntentService() {
super("aaa");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
//执行耗时任务
Log.d(TAG, "onHandleIntent: start");
String serviceName = intent.getStringExtra("serviceName");
if (TextUtils.equals(serviceName, "a")){
simulationTask();
Log.d(TAG, "onHandleIntent: simulationTask complete");
}
}
/**
* 模拟耗时任务
*/
private void simulationTask() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Destroy ChildIntentService");
}
}
2. 启动IntentService
public void start(View view) {
Intent intent = new Intent(MainActivity.this, ChildIntentService.class);
intent.putExtra("serviceName", "a");
startService(intent);
}
3. 打印log信息
2019-05-31 18:05:41.712 11300-11349/architecture.android.com.architecture D/qianwei: onHandleIntent: start
2019-05-31 18:05:46.713 11300-11349/architecture.android.com.architecture D/qianwei: onHandleIntent: simulationTask complete
2019-05-31 18:05:46.716 11300-11300/architecture.android.com.architecture D/qianwei: Destroy ChildIntentService
我们看到,当任务执行结束后IntentService确实会自动停止。
二、IntentService源码分析
在实现上,IntentService封装了HandlerThread和Handler,当IntentService第一次被启动,会调用它的onCreate方法,我们首先看它的onCreate方法实现:
@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);
}
onCreate方法会创建一个HandlerThread对象thread并调用它的start方法,利用这个HandlerThread的Looper创建ServiceHandler对象mServiceHandler,这样通过mServiceHandler发送的消息最终都会在HandlerThread中执行。
每次启动IntentService,它的onStartCommand方法都会调用一次,我们首先看看onStartCommand方法:
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
我们可以看到,onStartCommand方法中直接调用onStart方法:onStart方法只是把intent封装进一个消息,并通过mServiceHandler发送出去。我们继续看ServiceHandler的内部实现:
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);
}
}
ServiceHandler内部很简单,它在收到消息之后会把消息传递给onHandleIntent方法处理,onHandleIntent方法需要我们在子类中实现,它的作用是通过Intent区分具体任务并执行这些任务。当onHandleIntent方法结束后会调用IntentService的stopSelf(int startId)方法尝试停止服务,因为这个时候可能还有其他消息未处理,只有所有消息都处理完才会真的停止服务。
现在我们知道了,IntentService的内部是通过消息的方式请求HandlerThread执行任务,HandlerThread内部又是一种使用Handler的Thread,这就意味着IntentService和Looper一样是顺序执行后台任务的。
参考
《Android开发艺术探索》
转载于:https://www.jianshu.com/p/2d5dbb7fe202