IntentService和HandlerThread源碼講解

今天復習了一下IntentService的源碼,在這里記錄一下
先上源碼

package android.app;

import android.annotation.WorkerThread;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;

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

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            //標記2
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }

    public IntentService(String name) {
        super();
        mName = name;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //標記1
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

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

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

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @WorkerThread
    protected abstract void onHandleIntent(Intent intent);
}

然后來講解一下:

  1. IntentService里有一個自定義的Handler類,名字叫ServiceHandler,這個ServiceHandler是執(zhí)行異步任務(wù)的,為什么它可以執(zhí)行異步任務(wù)呢,看標記1,因為我們給這個ServiceHandler設(shè)置的Loop是HandlerThread的Loop,而HandlerThread是在新線程創(chuàng)建的Loop,所以我們這個ServiceHandler可以且僅可以執(zhí)行異步任務(wù)
  2. IntentService啟動以后,在onStart方法里可以看到會發(fā)送一個Message到HandlerThread的Looper,因為Looper是從HandlerThread取到的所以這個Looper在新線程,所以ServiceHandler的handleMessage也是在新線程執(zhí)行的,然后調(diào)用IntentService的抽象方法onHandleIntent,很明顯onHandleIntent方法里不能直接操作UI元素,因為該方法不是執(zhí)行在UI線程

再說一下HandlerThread,先放源碼:

package android.os;

import android.annotation.NonNull;
import android.annotation.Nullable;

public class HandlerThread extends Thread {
    int mPriority;
    int mTid = -1;
    Looper mLooper;
    private @Nullable Handler mHandler;

    public HandlerThread(String name) {
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    }
    
    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }
    

    protected void onLooperPrepared() {
    }

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }
    
    public Looper getLooper() {
        if (!isAlive()) {
            return null;
        }
        
        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    }

    @NonNull
    public Handler getThreadHandler() {
        if (mHandler == null) {
            mHandler = new Handler(getLooper());
        }
        return mHandler;
    }

    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }

    public boolean quitSafely() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quitSafely();
            return true;
        }
        return false;
    }
}

HandlerThread是繼承的Thread,在HandlerThread中有一個Loop類屬性,這個Loop會在HandlerThread的run方法中創(chuàng)建,注意到里面有一個notifyAll方法,為什么有這個方法呢,是因為HandlerThread還有一個getLooper方法,這個方法在IntentService的onCreate方法里被調(diào)用了,用來將HandlerThread的Loop傳遞給IntentService的ServiceHandler,但是因為Loop的創(chuàng)建是在run方法里,所以調(diào)用HandlerThread的getLooper方法時Loop不一定實例化了,所以getLooper方法里判斷線程正在執(zhí)行并且Loop為null的時候執(zhí)行wait方法,等待HandlerThread的run方法實例化了Loop以后調(diào)用notifyAll喚醒等待返回實例化了的Loop實例


流程就是這樣的,還有一些細節(jié),如:

  1. 停止IntentService的時候,onDestory會立即執(zhí)行,后續(xù)任務(wù)不會繼續(xù),看IntentService的onDestory也能看出來,方法里的mServiceLooper.quit();這句話清空了任務(wù),但是正在執(zhí)行的任務(wù)不會被打斷。
  2. 在IntentService的ServiceHandler類的handleMessage方法里,為什么執(zhí)行了stopSelf(msg.arg1);卻不會把當前任務(wù)的后續(xù)任務(wù)都停止了呢?是因為IntentService會嘗試通過stopSelf(int startId)來嘗試停止服務(wù)。之所以不用stopSelf()來停止服務(wù),是因為stopSelf()會立刻停止服務(wù),而stopSelf(int startId)則會等待所有的消息都處理完畢才回終止服務(wù)。一般來說,stopSelf(int startId)在嘗試停止服務(wù)之前會判斷最近啟動服務(wù)的次數(shù)是否和startId相等,如果相等則立刻停止服務(wù)。這些是文檔里說的,源碼我還沒看,有興趣可以自己看看。
  3. 細心的朋友應(yīng)該注意到IntentService中的2個屬性
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;
    
    加了volatile,這個關(guān)鍵字的作用是什么呢,這就涉及到JVM的執(zhí)行方式了,JVM虛擬機可能會給語句做優(yōu)化,雖然你語句寫的順序是A然后B,但是JVM可能給你優(yōu)化成先執(zhí)行B語句再執(zhí)行A語句,這樣的話下面的代碼
    mServiceLooper = thread.getLooper();//語句1
    mServiceHandler = new ServiceHandler(mServiceLooper);//語句2
    
    可能就有問題,因為JVM可能給你優(yōu)化成先執(zhí)行語句2再執(zhí)行語句1,這樣會導致什么后果呢,如果先執(zhí)行語句2的話,mServiceLooper是null,因為mServiceLooper = thread.getLooper();還沒有執(zhí)行,所以ServiceHandler傳參進去的是個null,然后再執(zhí)行語句1已經(jīng)沒有意義了,因為ServiceHandler已經(jīng)傳進去一個null參數(shù)了。而加上volatile能保證這2個語句在任何JVM里都能保證執(zhí)行順序先語句1后語句2。在IBM的官網(wǎng)有這部分的講解,可以自己再深入了解一下
  4. 為什么不建議通過 bindService() 啟動 IntentService?因為IntentService 源碼中的 onBind() 默認返回 null;不適合 bindService() 啟動服務(wù),如果你執(zhí)意要 bindService() 來啟動 IntentService,可能因為你想通過 Binder 或 Messenger 使IntentService 和 Activity 可以通信,這樣的話 onHandleIntent() 不會被回調(diào),因為相當于在你使用 Service 而不是 IntentService。
最后編輯于
?著作權(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)容

  • 簡介 首先我們先來了解HandlerThread和IntentService是什么,以及為什么要將這兩者放在一起分...
    jtsky閱讀 757評論 0 9
  • IntentService 在實際開發(fā)過程中會有這樣的一個需求,我們需要運行一個任務(wù),并且只需要在后臺默默運行即可...
    未見哥哥閱讀 414評論 0 3
  • 一、線程 主線程 主線程是指進程中所擁有的線程,在Java中默認情況下一個進程只有一個線程,這個線程就是主線程。 ...
    碼字與律動閱讀 399評論 0 2
  • Android中的線程 線程,在Android中是非常重要的,主線程處理UI界面,子線程處理耗時操作。如果在主線程...
    shenhuniurou閱讀 875評論 0 3
  • 我站在天幕下 黑夜里的藍,桃紅上的白 將我斑駁,斑駁 譏笑著我,一場癡夢為何 明明清醒,卻一往而深 明明苦難,卻毫...
    煙雨心清閱讀 951評論 38 19

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