Android IntentService 源碼解析

Service作為Android四大組件之一在項目開發(fā)中至關(guān)重要,一些耗時的業(yè)務(wù)都會用到Service,但是因為Service是在主線程當中的,所以要想在Service中做一些耗時的操作必須要自己創(chuàng)建Thread,還好Android提供了另外一個類就是我們要講的IntentService,IntentService是Service的子類,下面就讓我們通過源碼來看看它內(nèi)部的實現(xiàn),我們先來看看它的onCreate方法。

onCreate方法
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);
}

首先創(chuàng)建了一個HandlerThread,不了解HandlerThread的可以看看這篇文章(http://www.lxweimin.com/p/d1e0bb996601
啟動線程,創(chuàng)建ServiceHandler,通過onCreate方法可以看出它內(nèi)部封裝了一個
Handler,Looper,Thread的消息處理邏輯。

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

通過mServiceHandler發(fā)送了一條消息,然后回到handleMessage執(zhí)行邏輯

public void handleMessage(Message msg) {
       onHandleIntent((Intent)msg.obj);
       stopSelf(msg.arg1);
}

onHandleIntent就是我們要重寫的方法,這里處理我們的業(yè)務(wù)邏輯,stopSelf方法銷毀IntentService,如果這條消息是最后一條消息就銷毀,否則不銷毀,我們通過代碼來驗證下

public class TestService extends IntentService {

    public static final String TAG = "TestService";

    public TestService() {
        super("st");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.i(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand:" + startId);

        return super.onStartCommand(intent, flags,startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
}

通過startService啟動
startService(new Intent(EleActivity.this, TestService.class));
我們來看看打印結(jié)果

01-24 13:42:53.597 3172-3172/com.fear I/TestService: onCreate
01-24 13:42:53.598 3172-3172/com.fear I/TestService: onStartCommand:1
01-24 13:42:53.600 3172-3172/com.fear I/TestService: onDestroy

我們調(diào)用了onStartCommand方法一次發(fā)送一條消息,消息處理完銷毀自己。
那我們發(fā)送5條消息會是什么有樣子呢?

 for(int i = 0; i < 5; i++)
 {
      startService(new Intent(EleActivity.this, TestService.class));
 }

因為IntentService的onCreate只會調(diào)用一次,如果啟動5次是會調(diào)用5次onStartCommand方法,我們來看看打印結(jié)果

01-24 13:45:02.535 6363-6363/com.fear I/TestService: onCreate
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:1
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:2
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:3
01-24 13:45:02.537 6363-6363/com.fear I/TestService: onStartCommand:4
01-24 13:45:02.537 6363-6363/com.fear I/TestService: onStartCommand:5
01-24 13:45:02.542 6363-6363/com.fear I/TestService: onDestroy

可以看到調(diào)用了5次onStartCommand,發(fā)送了5條消息,消息都處理完,然后銷毀自己。

總結(jié)

IntentService是Service子類,重寫了onCreate方法和onStart方法,內(nèi)部封裝了Handler,Looper,Thread來處理消息,開發(fā)者可以通過復寫onHandleIntent來處理自己的業(yè)務(wù)邏輯。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容