美文网首页
Android SDK源码解析篇之IntentService使用

Android SDK源码解析篇之IntentService使用

作者: 非典型程序猿 | 来源:发表于2020-02-27 15:37 被阅读0次

    总结完HandlerThread的原理以后,今天开始总结IntentService的使用和原理解析。

    IntentService的特点

    IntentService继承于Service,与Service不同的是,当我们每次开启IntentService后,我们不需要去手动的再调用stopSelf()去关闭这个IntentService,当业务执行完毕后它会自动地关闭这个Service。

    使用

    首先需要写一个类继承自IntentService,并在onHandleIntent()方法中去实现我们想要在这个Service中的业务.至于为什么会在这个方法里实现,下面会通过源码告诉答案。

    public class MyIntentService extends IntentService {
    
        public MyIntentService() {
            super("MyIntentService");
        }
    
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            LogUtils.e("MyIntentService" , "开始执行IntentService");
        }
    }
    

    定义好类以后我们还需要在AndroidMainfest.xml application标签中声明一下这个Service

    <service android:name=".MyIntentService"></service>
    

    接着我们只需要在使用时调用

    Intent intent = new Intent(this,MyIntentService.class);
            startService(intent);
    

    打印结果如下

    02-27 15:12:48.988 3690-3733/com.txVideo.demo E/MyIntentService: 开始执行IntentService
    

    使用过程还是比较简单的,那么我们接下来看看它的实现原理,首先我们打开IntentService类看看与Service有些什么不同的地方
    打开源码,我就看到了两个变量

        private volatile Looper mServiceLooper;
        @UnsupportedAppUsage
        private volatile ServiceHandler mServiceHandler;
    

    通过前面总结的Handler和HandlerThread的知识,似乎猜到了一点东西,那么我们接着往下看,在复写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);
        }
    

    这很明显嘛,这不就是上篇文章HandlerThead的使用嘛,通过新开一个HandlerThread线程,开启Looper的循环,然后让mServiceHandler去处理接收到消息时的业务,那么我们再看一下这个mServiceHandler的对象:

     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就是一个Handler的子类,不过它在处理消息时,触发了onHandleIntent()方法,这不就是刚刚那个IntentService中的处理业务的onHandleIntent()方法吗?然后调用了stopSelf()方法,这个方法是Service中的,也就是停止Service所用的。那么IntentService如何触发了onHandleIntent()就知道了。但是HandlerThread是需要被触发的,所以我们看看它是如何被触发的.然后我就看到了onStart()方法。

    @Override
        public void onStart(@Nullable Intent intent, int startId) {
            Message msg = mServiceHandler.obtainMessage();
            msg.arg1 = startId;
            msg.obj = intent;
            mServiceHandler.sendMessage(msg);
        }
    

    这就是一个很简单的发送消息的过程嘛。
    最后我们知道HandlerThread使用完毕以后需要关闭它的循环,当然,IntentService也做了

    @Override
        public void onDestroy() {
            mServiceLooper.quit();
        }
    

    所以到这里,整个IntentService的运行原理我们就了解了,梳理一下:

    • 首先我们创建IntentService的实例对象时,触发了onCreate()方法,它开启了一个HandlerThread,并且调用了start()方法,开启了Looper循环,接收消息,然后创建了一个Handler实例,这个实例会处理接收到的消息,每处理一次消息,会触发一次onHandleIntent()方法,然后关闭调用stopSelf()关闭这个服务.最后在onDestroy()方法中关闭这个Looper循环。
    • 我们在onStart()方法里向这个Handler发送消息,用来触发Handler中的handlerMessage()方法。
      梳理完整个原理,大家应该也就知道为什么onHandleIntent()方法是处理IntentService()业务的地方,为什么每次这个IntentService执行完业务都会自己关闭了.
      今天的IntentService使用及源码解析就整理到这里啦,觉得有帮助记得点个赞~

    相关文章

      网友评论

          本文标题:Android SDK源码解析篇之IntentService使用

          本文链接:https://www.haomeiwen.com/subject/kpathhtx.html