Android非同步載入全解析之IntentService

來源:互聯網
上載者:User

Android非同步載入全解析之IntentService
Android非同步載入全解析之IntentService搞什麼IntentService前面我們說了那麼多,非同步處理都使用欽定的AsyncTask,再不濟也使用的Thread,那麼這個IntentService是個什麼鬼。 相對與前面我們提到的這兩種非同步載入的方式來說,IntentService有一個最大的特點,就是——IntentService不受大部分UI生命週期的影響,它為後台線程提供了一個更直接的操作方式。不過,IntentService的不足主要體現在以下幾點:
不可以直接和UI做互動。為了把他執行的結果體現在UI上,需要發送給Activity。工作任務隊列是順序執行的,如果一個任務正在IntentService中執行,此時你再發送一個工作要求,這個任務會一直等待直到前面一個任務執行完畢。正在執行的任務無法打斷。建立IntentService當我們建立一個類並繼承IntentService後,通過IDE的提示,我們基本可以產生如下所示的代碼:

package com.imooc.intentservicetest;import android.app.IntentService;import android.content.Intent;public class MyIntentService extends IntentService {    /**     * Creates an IntentService.  Invoked by your subclass's constructor.     *     * @param name Used to name the worker thread, important only for debugging.     */    public MyIntentService(String name) {        super(name);    }    public MyIntentService() {        super(MyIntentService);    }    @Override    protected void onHandleIntent(Intent intent) {    }}

PS:需要注意的是,系統會提示我們產生一個帶參數的構造方法,另外,我們還需要定義一個無參數的構造方法,並調用一個參數的構造方法。所以,一個帶參數的構造方法其實是可以刪掉的。
最重要的就是:
@Overrideprotected void onHandleIntent(Intent intent) {}

在這個方法裡面,我們從intent中擷取資料,並進行相應的操作。
ps:IntentService繼承自Service,但是我們不需要手動調用onStartCommand()等Service回調方法。
申明IntentService我們需要在Mainifest檔案中對IntentService進行申明:


與申明一個Service類似,但是卻不需要申明,因為發送任務給IntentService的Activity需要使用顯式Intent,所以不需要filter。這也意味著只有在同一個app或者其他使用同一個UserID的組件才能夠訪問到這個IntentService。
啟動IntentService啟動IntentService與啟動Service基本類似,而且我們可以在Intent中傳入相關的參數。例如:
package com.imooc.intentservicetest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = null;        for (int i = 0; i < 10; i++) {            intent = new Intent(this, MyIntentService.class);            intent.putExtra(xys,  + i);            startService(intent);        }    }}

可以看見,我們start了10個IntentService,最後執行的結果如下:
05-13 17:14:53.515  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent005-13 17:14:55.528  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent105-13 17:14:57.540  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent205-13 17:14:59.544  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent305-13 17:15:01.556  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent405-13 17:15:03.569  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent505-13 17:15:05.570  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent605-13 17:15:07.574  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent705-13 17:15:09.577  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent805-13 17:15:11.581  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent9

所以說,啟動IntentService之後,它將預設產生一個Worker Thread,並將這些IntentService依次放入隊列,每次取出一個進行執行,當執行完畢後,這個IntentService會自動stop自己,當所有intent都執行完畢後,服務就結束了,不需要自己手動來結束。

IntentService修改UIIntentService如果要進行UI的修改,那麼只能通過Handler來實現,或者使用廣播機制來通知修改UI。
IntentService與AsyncTask的區別
對於非同步更新UI來說,IntentService使用的是Serivce+handler或者廣播的方式,而AsyncTask是thread+handler的方式。 AsyncTask比IntentService更加輕量級一點。 Thread的運行獨立於Activity,當Activity結束之後,如果沒有結束thread,那麼這個Activity將不再持有該thread的引用。 Service不能在onStart方法中執行耗時操作,只能放在子線程中進行處理,當有新的intent請求過來都會線onStartCommond將其入隊列,當第一個耗時操作結束後,就會處理下一個耗時操作(此時調用onHandleIntent),都執行完了自動執行onDestory銷毀IntengService服務。
 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.