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。

相關文章
相關標籤/搜索