IntentService 原理解析

305 阅读2分钟

IntentService 适用于处理异步请求,客户端通过startService(Intent)的方式发送请求,服务按需启动,使用工作线程来顺序处理请求,当所有的请求处理完毕后自动退出。 IntentService继承自Service,并且是个抽象类,子类需继承才能使用。它比较适合用来执行优先级高的耗时任务,因为优先级高所以不容易被杀死。下面我们来看一下它的实现原理。

  1. 概览
public abstract class IntentService extends Service {
    1. 成员变量looper
    private volatile Looper mServiceLooper;
    2. 成员变量Handler
    private volatile ServiceHandler mServiceHandler;
    private String mName;
    private boolean mRedelivery;

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }
    3. mServiceHandler 处理消息后关闭自己。
        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }
  1. 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);
    }

HandlerThread在https://juejin.cn/post/6847902222790082573中分析过,在onCreate中主要是开启了异步线程,并且初始化了Handler,那么以后Handler发送的消息都会发送到这个异步线程中。

  1. onStartCommand方法
   @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }
    
   @Override
    public void onStart(@Nullable Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
    }

将Intent包装成Message,通过mServiceHandler发送到消息队列中,那么这个消息的处理者自然也就是mServiceHandler本身,也就是第1步中定义的onHandleIntent((Intent)msg.obj);方法。 每次启动IntentService,它的onStartCommand都会被调用1次,然后会处理每个intent

  1. onHandleIntent((Intent)msg.obj);
/**
     * 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);

这是个抽象的方法,需要子类自己实现。根据注释,onHandleIntent执行在工作线程中,并且每次只执行一个工作请求。多次的请求会顺序执行,当所有的请求都执行完毕后,Service会自行停止。