IntentService源碼解析

前幾篇文章帶領(lǐng)大家對HandlerThread的用法及原理進(jìn)行了深入探索,為了鞏固大家對HandlerThread的認(rèn)識,讓大家對HandlerThread的理解更上一層臺階,本篇文章將向大家介紹與HandlerThread關(guān)系密切的一大神器---IntentService,大家可以帶著以下三個問題去看接下來的文章:1.為什么說IntentService和HandlerThread有著密切的關(guān)系? 2.為什么IntentService可被稱作Android的一大神器,它和傳統(tǒng)的Service有何區(qū)別? 3. IntentService的底層實(shí)現(xiàn)原理是怎樣的?

我們知道,Service中的代碼是運(yùn)行在主線程中的,如果在Service中直接進(jìn)行耗時(shí)操作,就很有可能出現(xiàn)ANR(Application Not Responding)。所以,我們想在Service中進(jìn)行耗時(shí)操作,耗時(shí)邏輯一定要放在子線程中運(yùn)行,代碼如下:

public class FirstService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        new Thread(new Runnable(){
            @Override
            public void run(){
                //處理耗時(shí)邏輯
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }
}

上面的服務(wù)一旦啟動后會一直運(yùn)行,必須調(diào)用stopService()或stopSelf()才可以讓服務(wù)停止,代碼如下:

public class SecondService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        new Thread(new Runnable(){
            @Override
            public void run(){
                //處理耗時(shí)邏輯
                stopSelf();
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }
}

上面的寫法雖然并不復(fù)雜,但是還是會有不少程序員在Service中處理耗時(shí)邏輯時(shí)忘記開啟子線程或者忘記停止Service,Android中為了解決
這兩個問題,提升開發(fā)人員的效率,引入了IntentService這一神器!

我們先來看一下IntentService在官方文檔中的介紹:


文檔中講得很清楚,IntentService繼承自Service類,能夠用來處理異步的請求,客戶端可通過startService(Intent)來發(fā)送請求,Service會根據(jù)需要被啟動,使用一個工作者線程輪流處理每個請求,并且當(dāng)它處理完所有的請求后會停止它自身。

IntentService中有一個很重要的方法: onHandleIntent,看一下官方文檔對它的介紹:



當(dāng)某個請求需要處理時(shí),這個方法會在工作者線程被調(diào)用,一次僅僅會有一個請求被處理,但是處理過程會運(yùn)行在工作者線程(獨(dú)立于其他應(yīng)用程序邏輯運(yùn)行)。因此,如果某段代碼需要執(zhí)行很長時(shí)間,它會阻塞住其他提交到該IntentService的請求,但是不會阻塞住其他任何東西。當(dāng)所有的請求被處理完成之后,IntentService會停止它自身,因此你不應(yīng)該手動調(diào)用stopSelf()方法。

說了這么多,大家對IntentService的認(rèn)識可能還是很模糊,下面通過一個具體的例子讓大家深入地體會下:
我們模擬一個多張圖片下載的場景,點(diǎn)擊“Add Download Task”按鈕可以增加圖片下載任務(wù),下方也會增加一條記錄代表新增的下載任務(wù),當(dāng)下載任務(wù)完成時(shí),相應(yīng)的任務(wù)記錄也會進(jìn)行更新,運(yùn)行效果如下:

DownloadImageService中的代碼:

public class DownloadImageService extends IntentService {

    private static final String ACTION_DOWNLOAD_IMAGE="com.example.downloadimage.action.DOWNLOAD_IMAGE";
    public static final String EXTRA_DOWNLOAD_IMAGE="com.example.downloadimage.extra.DOWNLOAD_IMAGE";
    public DownloadImageService() {
        super("DownloadImageService");
    }
    
    public static void startDownload(Context context,String path){
        Intent intent=new Intent(context,DownloadImageService.class);
        intent.setAction(ACTION_DOWNLOAD_IMAGE);
        intent.putExtra(EXTRA_DOWNLOAD_IMAGE,path);
        context.startService(intent);
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        if(intent!=null){
            if(intent.getAction().equals(ACTION_DOWNLOAD_IMAGE)){
                String path=intent.getStringExtra(EXTRA_DOWNLOAD_IMAGE);
                handleDownloadTask(path);
            }
        }
    }

    private void handleDownloadTask(String path) {
        //模擬耗時(shí)操作
        try{
            Thread.sleep(3*1000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        //利用廣播通知主界面更新
        Intent intent=new Intent(MainActivity.RESULT_DOWNLOAD_IMAGE);
        intent.putExtra(EXTRA_DOWNLOAD_IMAGE,path);
        sendBroadcast(intent);
    }
    
    @Override
    public void onCreate(){
        super.onCreate();
    }
    
    @Override
    public void onDestroy(){
        super.onDestroy();
    }
}

MainActivity中的代碼:

public class MainActivity extends ActionBarActivity {
    
    public static final String RESULT_DOWNLOAD_IMAGE="com.example.downloadimage.result.DOWNLOAD_IMAGE";
    private Button addTaskBtn;
    private LinearLayout layoutContainer;
    private int i=0;
    private BroadcastReceiver resultReceiver=new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent!=null){
                if(intent.getAction().equals(RESULT_DOWNLOAD_IMAGE)){
                    String path=intent.getStringExtra(DownloadImageService.EXTRA_DOWNLOAD_IMAGE);
                    TextView tv=(TextView)layoutContainer.findViewWithTag(path);
                    tv.setText(path+" successfully downloaded");
                }
            }
        }
        
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addTaskBtn=(Button)findViewById(R.id.addTaskBtn);
        layoutContainer=(LinearLayout)findViewById(R.id.layoutContainer);
        addTaskBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                String path="www.lemon.com/imgs/img"+(++i)+".png";
                DownloadImageService.startDownload(MainActivity.this,path);
                TextView tv=new TextView(MainActivity.this);
                tv.setText(path+" is downloading");
                tv.setTag(path);
                layoutContainer.addView(tv);
            }
        });
        registerReceiver(resultReceiver);
    }
    
    private void registerReceiver(BroadcastReceiver receiver) {
        IntentFilter intentFilter=new IntentFilter(RESULT_DOWNLOAD_IMAGE);
        registerReceiver(receiver,intentFilter);
    }
    
    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(resultReceiver);
    }

}

主布局文件的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/layoutContainer"
    tools:context="com.example.downloadimage.MainActivity" >

   <Button
      android:id="@+id/addTaskBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Add Download Task"
       />

</LinearLayout>

其實(shí),邏輯還是相當(dāng)簡單的,每當(dāng)點(diǎn)擊“Add Download Task”按鈕時(shí),會去調(diào)用DownloadImageService的startDownload方法,在startDownload中,會調(diào)用startService方法去啟動我們的DownloadImageService,startService方法的參數(shù)是一個intent,該intent中封裝了我們這回要執(zhí)行的任務(wù)類型(action)以及執(zhí)行任務(wù)所需的參數(shù)(Extra),之后會執(zhí)行到onHandleIntent方法,此時(shí)的onHandleIntent方法是運(yùn)行在子線程中的,在onHandleIntent方法中,會去執(zhí)行圖片下載的具體任務(wù),下載完成后,會發(fā)送一個廣播通知主界面相關(guān)任務(wù)記錄進(jìn)行更新。
講到這里,大家對IntentService的基本用法應(yīng)該有一個大致的了解了吧,但是目前的理解層次還不能完全回答之前提出的三個問題,看來,只能讓源碼君幫幫我們了_

IntentService的源碼如下:

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

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


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


    public void setIntentRedelivery(boolean enabled) {
        mRedelivery = enabled;
    }

    @Override
    public void onCreate() {
                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);
    }


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


    protected abstract void onHandleIntent(Intent intent);
}

IntentService中有兩個成員變量較為重要,一個是Looper類型的mServiceLooper,還有一個是ServiceHandler類型的mServiceHandler。這個ServiceHandler是個啥東東呢?我們看一下它的源碼,ServiceHandler繼承自Handler,內(nèi)部提供了一個以Looper為參數(shù)的構(gòu)造函數(shù),也就是說,如果我們傳入的Looper是子線程的Looper,ServiceHandler的handleMessage方法就會運(yùn)行在子線程中。我們再去看一下ServiceHandler的handleMessage方法中有些什么,原來是依次調(diào)用了onHandleIntent,stopSelf方法,看到這里,對異步消息處理機(jī)制及HandlerThread掌握較牢固的朋友應(yīng)該都能猜到接下來的源碼了,不要激動,咱們繼續(xù)往下看_

接下來看到IntentService的onCreate方法,在onCreate中,先去創(chuàng)建并啟動了HandlerThread,然后用該HandlerThread的Looper去初始化我們的ServiceHandler,這樣ServiceHandler就用的是子線程的Looper,其handleMessage方法自然是運(yùn)行在子線程中的,所以handleMessage中的onHandleIntent和stopSelf方法自然也是運(yùn)行在子線程中的。

當(dāng)我們調(diào)用startService方法啟動服務(wù)時(shí),首先會去調(diào)用onStartCommand方法,在onStartCommand方法中,會再去調(diào)用onStart方法,并將intent和startId傳入,我們重點(diǎn)看一下onStart方法。在onStart方法中,會先去通過ServiceHandler對象去獲取一條消息,之后將消息的arg1字段設(shè)置為startId,obj字段設(shè)置為intent,最后用ServiceHandler對象將我們這條消息發(fā)送出去。

之后消息便會輾轉(zhuǎn)到handleMessage方法中了,注意,此時(shí)handleMessage是運(yùn)行在子線程中的,在handleMessage方法中,會將消息的obj字段取出強(qiáng)轉(zhuǎn)為Intent并作為參數(shù)傳入onHandleIntent方法中,之后將消息的arg1字段取出并作為參數(shù)傳入stopSelf中。

當(dāng)請求全部執(zhí)行完成后,會銷毀我們的Service。銷毀Service時(shí)會回調(diào)onDestroy方法,在onDestroy方法中會調(diào)用mServiceLooper.quit()來釋放我們的Looper。

好了,到這里IntentService的源碼就基本分析完畢了,大家對IntentService的理解是不是又更上一層樓了呢_

參考:《第一行代碼 Android》
http://blog.csdn.net/lmj623565791/article/details/47143563

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

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

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