Android IntentService 的使用和解析

[TOC]java

1.IntentService的简单使用

使用IntentService只需自定义类继承该类,并在构造函数中调用super()方法和实现 onHandleIntent()方法便可。
以下代码所示:android

class MyIntentService:IntentService("MyIntentService") {
    override fun onHandleIntent(intent: Intent?) {
        //在这里实现一些业务
        LogUtil.i("hugo","onHandleIntent")
        Thread.sleep(5000)
    }
}

// AndroidManifest 中配置
<application>
 <service android:name=".MyIntentService"/>
</application>

//MainActivity 
btn_intent_service.setOnClickListener {
            startService(Intent(this,MyIntentService::class.java))
        }

复制代码

这样就能够了。是否是很简单,让咱们来看一看IntentService是怎么实现的吧。app

2.IntentService的具体实现

1.在IntentService 的构造方法中须要传入一个nameide

/** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */
    public IntentService(String name) {
        super();
        mName = name;
    }
复制代码

而这个name 是用于 建立线程时给线程命名的。函数

2.在IntentService 首次建立时时系统会调用 onCreate() 方法 IntentService在这个方法中会建立一个建立一个了一个线程,并启动了该线程 以下:oop

@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();
        //获取线程的looper
        mServiceLooper = thread.getLooper();
       //建立一个Handler
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }
复制代码

3.在别的组件启动IntentService时,IntentService会调用onStartCommand() 方法this

@Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }
    
复制代码

能够看到IntentService 在 onStartCommand() 方法中调用了 onStart()方法spa

@Override
    public void onStart(@Nullable Intent intent, int startId) {
    //获取一个Message
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        //把Message发送给 Handler
        mServiceHandler.sendMessage(msg);
    }
复制代码

这个onStart() 方法主要就是获取一个Message 并把Intent存入其中 把Message发送给Handler线程

3.那咱们来看一下Handler是怎么实现的debug

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);
        }
    }
    
    //IntentService.java
        protected abstract void onHandleIntent(@Nullable Intent intent);
复制代码

能够看到Handler里面的实现也很简单,就是把Message中传过来的Intent传入 IntentService的抽象方法onHandleIntent() 中,这个方法就是咱们实现进行业务操做的方法,而后就是stopSelf() 方法关闭这个Service。

3.总结

看了IntentService的源码后发现IntentService主要就是在建立时建立了一个线程来进行来进行业务做业。 在调用 onStartCommand() 方法时把Intent发送给Handler,在做业完成后会调用 stopSelf() 方法关闭Service。

相关文章
相关标签/搜索