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服務。