Android_IntentService源码梳理

IntentService是Android提供和的一个抽象类.按需处理异步请求,客户端发送请求通过startService(intent)调用,这个服务根据需要被启动,使用工作线程来处理每个Intent,并在处理完任务后停止自身.

以上就是google对IntentService的简短介绍【我翻译的】.那我们今天就从源码的角度对IntentService进行梳理:

首先我们已知IntentService是一个抽象类,本身又是继承了Service,生命周期和Service组件很类似;然后经过对源码的简单梳理,看到intentService里面其实相当于一个我们之前分析的HandlerThread。并且在执行完成任务后会自我了断。

流程梳理

在IntentSercice第一次创建的时候会走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,接着启动HandlerThread;然后通过HandleThread中的looper创建了一个ServiceHandler,IntentService的onCreate ()方法里面就做了这些事......【回忆一下HandleThread的使用,IntetService其实就是基于handlerThread的封装使用】。

然后,每次使用IntentService 时,就会调用onStartCommand()方法:

/**
    * 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);
    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);
}

google 提醒我们,不要再我们自己的代码里重写onStartCommand()方法。相反,我们需要重写onHandleIntent()方法,当IntentService接收到任务请求时,系统会调用onHandleIntent();

这个onStartCommand()方法里调用了onStart()方法;在onStart()方法中主要是把传进来的Intent和StartId封装成Message,通过mServiceHandler发送到消息队列。mServiceHandelr是ServiceHandlerThread对象。接下来我们要看看刚才发送的消息后来怎么处理的:

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);
    }
}

这个Servicehandelr继承Handler,所以我们看卡这个handleMessage()方法中是怎处理消息;我们可以看待,这里主要有两个方法:

  1. onHandeIntent(intent)
  2. stopSelf(startId)

1.  onHandeIntent(intent)

在上面讲到onStartCommand()方法时,我们就看到Google提醒我们要在自己的代码里面重写onHandleIntent()方法;我的任务处理逻辑都是在onHandleIntent()里面。在onHandleIntent()方法执行完成后,就会走到stopSelf()

扫描二维码关注公众号,回复: 8632118 查看本文章

1.  stopSelf(startId)

当任务完成,就会走stopSelf(startId)方法,这就是IntentService自我了断的的方法,不用我们再做任何处理了。

使用 

一般我们使用IntentService,需要定义一个我们自己的任务类,然后继承InsetenService,重写方法;然后再外面startService(intent),就可以使用IntentService;我们只管启动服,不用关闭。还是很方便的:

    public class MyIntentService extends IntentService{
//        private final String TAG = MyIntentService.class.getSimpleName();
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         *
         * @param name Used to name the worker thread, important only for debugging.
         */
        public MyIntentService(String name) {
            super(name);
        }

        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            // TODO 添加工作任务得的具体处理逻辑
        }
    }
    Intent intent = new Intent(this,MyIntentService.class);
    startService(intent);


总结

IntentService我们可以当成是 HandleThread和Service组件的综合还使用:

  1. 在生命周期和主要方法上和Service组件相似,
  2. 在任务处理上和HandlerThread相似
  3. 使用方便,我们只管启动不管关闭,【活好不纠缠的标杆】

 

 

 

 

发布了41 篇原创文章 · 获赞 35 · 访问量 4303

猜你喜欢

转载自blog.csdn.net/weixin_38140931/article/details/103244708