IntentService源碼講解

1. 前言

Broadcast Receiver用於接收並處理廣播通知,衆所周知,onReceive(Context curContext, Intent broadcastMsg)方法用於接受通知,並在該方法體內處理消息,可是onReceive中代碼的執行時間不要超過5s,不然會報ANR錯誤,致使程序異常。通常這種狀況,會有兩個方案:
1)在onReceive方法中開啓子線程,作耗時操做android

2)在onReceive方法中啓動新的Service,作耗時操做
bash

第一種方式很明顯不可行,由於BroadCastReceiver的生命週期很短,一旦被回收後,onReceive方法中開啓的線程就成了空線程,當手機資源緊張回收資源時,空線程會被系統回收,致使子線程中的任務並不能順利執行完畢就被殺死。app

通常咱們會用第二種方式,開啓一個服務,可是咱們知道servce也不能作耗時操做,難道咱們在service再開啓一個子線程嗎?Google提供了IntentService類,幫咱們作好了一切,咱們只須要使用IntentSerivce便可,他內部幫咱們開啓了子線程,咱們只須要重寫onHandleIntent方法,在onHandleIntent方法中執行耗時操做。其實IntentService是使用了HandlerThread實現子線程耗時操做,若是對HandlerThread不熟悉,能夠看上篇詳細的介紹。
less

2.使用方法

1)繼承IntentService異步

public class MyService extends IntentService 複製代碼

2)重寫onHandleIntent()方法ide

@Override
    protected void onHandleIntent(Intent intent) {
        //執行耗時操做
    }複製代碼

所有代碼以下所示:oop

/**
 * Created by Administrator on 2018/5/31.
 */
public class MyService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public MyService(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { //執行耗時操做 } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public void onCreate() { super.onCreate(); } }複製代碼

咱們知道,當啓動service後,會執行oncreate()->onStartCommand->onStart()方法,可是爲何咱們的耗時操做卻寫在onHandlleIntent方法中,他內部是如何處理的呢,咱們看下IntentService的源碼。
學習

3.源碼講解

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) {
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }

    /**
     * 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; } /** * Sets intent redelivery preferences. Usually called from the constructor * with your preferred semantics. * * <p>If enabled is true, * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_REDELIVER_INTENT}, so if this process dies before * {@link #onHandleIntent(Intent)} returns, the process will be restarted * and the intent redelivered. If multiple Intents have been sent, only * the most recent one is guaranteed to be redelivered. * * <p>If enabled is false (the default), * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent * dies along with it. */ public void setIntentRedelivery(boolean enabled) { mRedelivery = enabled; } @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); } @Override public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } /** * You should not override this method for your IntentService. Instead, * override {@link #onHandleIntent}, which the system calls when the IntentService * receives a start request. * @see android.app.Service#onStartCommand */ @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(); } /** * Unless you provide binding for your service, you don't need to implement this
     * method, because the default implementation returns null. 
     * @see android.app.Service#onBind
     */
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * This method is invoked on the worker thread with a request to process.
     * Only one Intent is processed at a time, but the processing happens on a
     * worker thread that runs independently from other application logic.
     * So, if this code takes a long time, it will hold up other requests to
     * the same IntentService, but it will not hold up anything else.
     * When all requests have been handled, the IntentService stops itself,
     * so you should not call {@link #stopSelf}.
     *
     * @param intent The value passed to {@link
     *               android.content.Context#startService(Intent)}.
     */
    protected abstract void onHandleIntent(Intent intent);
}複製代碼

代碼很是少,咱們開始分析。ui

1)首先,當啓動IntentService的時候,會調用他的onCreate方法,若是看了上篇HandlerThread的使用代碼,你應該會很是眼熟,他其實就是在建立HandlerThread,並經過建立出來的HandlerThread獲取其中的Looper,經過Looper建立出mServiceHandler對象,由於Looper是在子線程中建立的,咱們用該Looper對象建立出來的Handler天然持有異步線程的Looper,全部很正常,Hander的handleMessage方法能夠執行耗時的異步操做。(若是仍是不明白,能夠詳細看上篇,對於HandlerThread的介紹)。this

2)onCreate方法執行完畢後,會執行onStartCommand方法,onStartCommand又會調用onStart方法,咱們看下onStart方法體,裏面其實就作了一件事,發送handler消息。接下來,確定是看該mServiceHandler類的handlerMessage方法在哪裏。mServiceHandler是內部類,handlerMessage方法裏面調用了onHandleIntent方法,由於handlerMessage是在異步線程中,因此onHandleIntent方法也在異步線程中,而且onHandleIntent是個抽象方法,咱們只要重寫onHandleIntent方法體,便可實現異步線程處理耗時操做。

3)在onHandleIntent方法後面,咱們看到,會再調用stopSelf(msg.arg1)方法,stopSelf(int startId)會等待全部消息都處理完後才終止服務,也就是說異步線程結束後,服務會本身關閉。

4)若是你屢次startService,這時候,Service的生命週期是這樣的:oncreate()->onStartCommand->onStart()>onStartCommand->onStart()>onStartCommand->onStart()...->onHandlerIntent()->onHandlerIntent()->onHandlerIntent(),很容易理解,這就是Service的生命週期,至於爲何onHandlerIntent方法爲何會屢次被調用,由於每次調用onStart(),都會發送一次handler消息,而屢次調用會,message會按照時間順序存儲在messageQueue單向鏈表中,Looper對象會不停的取出Message處理,即回調HandlerMessage方法,從而執行到onHandlerIntent方法,這邊不清楚的,能夠看前面源碼角度講解Android消息處理機制(Handler、Looper、MessageQueue與Message)。當多個消息都處理結束後,stopSelf(msg.arg1)方法執行後,service關閉,整個流程結束。

------------------------------------------------------------------------------------------------

若有錯誤歡迎指出來,一塊兒學習。

相關文章
相關標籤/搜索