IntentService源碼分析

IntentService跟ThreadHandler一樣,也是Google為了方便開發(fā)者使用Service封裝的一個類。

特點

  1. 通過Context的startService(Intent),創(chuàng)建一個工作線程處理異步請求
  2. 異步的,串行處理每個Intent請求,處理完后自行停止Service

不瞎bb了,源碼能解釋一下

源碼分析

先看一下構(gòu)造和成員屬性,

public abstract class IntentService extends Service {
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;
    private String mName;
    private boolean mRedelivery;

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

IntentService繼承Service,被聲明為 abstract class, 所以我們只能繼承該類,不能直接使用。
有幾個比較重要的成員屬性Looper和ServiceHandler。Looper的作用我們就不啰嗦了,前面的文章已經(jīng)介紹過了。
ServiceHandler是IntentService里的一個內(nèi)部類,繼承Handler,實際用來處理Intent請求的方式。子類重寫onHandleIntent(Intent)處理Intent,之后調(diào)用stopSelf停止Service。

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

沒錯,聰明的你看到這里也大致猜到了,IntentSeries內(nèi)部其實就是使用Handler,Looper的機制,異步處理Intent請求的。不過,我們還得繼續(xù)分析源碼,因為還有內(nèi)容值得我們學(xué)習(xí)。

按Service的生命周期來看,會先調(diào)用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);
    }

這里創(chuàng)建了一個HandlerThread和一個ServiceHandler,并將HandlerThread里的Looper傳給ServiceHandler,是不是挺熟悉的? 恩,就是我們上個章節(jié)對HandlerThread的介紹里面的內(nèi)容,這里就不多廢話了。

接下來我們繼續(xù)看 onStart,onStartCommand方法

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

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

最終調(diào)用的是onStart方法, 從mServiceHandler里obtain一條Message, 將startId和Intent通過Handler傳遞給Looper,MessageQueue。 根據(jù)Handler的機制處理消息,在IntentService destroy的時候也將Looper停止。

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

所以,我們在使用IntentService的時候,只需要繼承IntentService,并重寫onHandleIntent方法,在需要的使用通過Content startService發(fā)送一個異步請求就可以了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容