IntentService 示例與詳解

為什么要用IntentService

IntentService 與 Service 相比的好處:一方面不需要自己去 new Thread 了;另一方面不需要考慮在什么時候關閉該 Service 了。


源碼解析

當IntentService第一次啟動,它的onCreate方法會被調用,該方法會創建一個HandlerThread,然后使用它的Looper來構造一個Handler對象mServiceHandler,這樣通過mServiceHandler發送的消息最終都會在HandlerThread中執行。

@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 ,Handy class for starting a new thread that has a looper。
     * 因為HandlerThread是一個Thread,我們調用其start方法就會調用run方法,其中會通過 Looper.myLooper()獲取當前線程的looper對象
     */
    HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
    thread.start();

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

private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        /**
         * use the provided {@link Looper} instead of the default one.
         */
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        onHandleIntent((Intent)msg.obj);
        /**
         * 自己停止Service服務
         */
        stopSelf(msg.arg1);
    }
}

每次啟動IntentService,它的onStartCommand方法就會被調用一次,onStartCommand調用了onStart方法。

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

IntentService僅僅通過mServiceHandler發送了一條消息,這個消息會在HandlerThread中處理。mServiceHandler收到消息后,會將Intent對象傳遞給onHandleIntent方法處理。

private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        /**
         * use the provided {@link Looper} instead of the default one.
         */
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        onHandleIntent((Intent)msg.obj);
        /**
         * 嘗試自己停止Service服務
         */
        stopSelf(msg.arg1);
    }
}

當onHandleIntent方法執行結束之后,IntentService會嘗試通過stopSelf(int startId)來嘗試停止服務。之所以不用stopSelf()來停止服務,是因為stopSelf()會立刻停止服務,而stopSelf(int startId)則會等待所有的消息都處理完畢才回終止服務。一般來說,stopSelf(int startId)在嘗試停止服務之前會判斷最近啟動服務的次數是否和startId相等,如果相等則立刻停止服務。

2. 為什么不建議通過 bindService() 啟動 IntentService?
@Override
public IBinder onBind(Intent intent) {
    return null;
}

IntentService 源碼中的 onBind() 默認返回 null;不適合 bindService() 啟動服務,如果你執意要 bindService() 來啟動 IntentService,可能因為你想通過 Binder 或 Messenger 使得 IntentService 和 Activity 可以通信,這樣那么 onHandleIntent() 不會被回調,相當于在你使用 Service 而不是 IntentService。

3. 為什么多次啟動 IntentService 會順序執行事件,停止服務后,后續的事件得不到執行?

IntentService 中使用的 Handler、Looper、MessageQueue 機制把消息發送到線程中去執行的,所以多次啟動 IntentService 不會重新創建新的服務和新的線程,只是把消息加入消息隊列中等待執行,而如果服務停止,會清除消息隊列中的消息,后續的事件得不到執行。

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

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

參考資料

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

推薦閱讀更多精彩內容

  • 前言:本文所寫的是博主的個人見解,如有錯誤或者不恰當之處,歡迎私信博主,加以改正!原文鏈接,demo鏈接 Serv...
    PassersHowe閱讀 1,446評論 0 5
  • [文章內容來自Developers] Service是一個可以在后臺執行長時間運行操作而不提供用戶界面的應用組件。...
    岳小川閱讀 888評論 0 7
  • 我這一生,雖然性格倔強,但限于身份,表現還算平和,除了寧可餓死也不排隊吃飯,恐怕就是個人爭吵的次數也屈指可數。單位...
    風沙迷了眼睛閱讀 148評論 0 0
  • 我覺得努力更重要。根據「一萬小時定律」要成為某個領域的專家,需要10000小時?,F在的大家無論你選擇了哪一行,你如...
    Jinyuba閱讀 178評論 0 0
  • 最近快要玩瘋了,已經宅不住了,每天不是出去玩就是在出去玩的路上。我一直在想我為什么要一直想著出去玩,不惜...
    成長中的Ivy閱讀 414評論 0 2