在Android的四大組件中,Service排行老二,在Android中的主要作用是后臺服務,進行與界面無關的操作。由于Service運行在主線程,所以進行異步操作需要在子線進行。為此Android為我們提供了IntentService。 IntentService是一個抽象類,繼承至Service,主要方便我們新建工作線程進行異步操作。提交任務到IntentService時,異步任務以串行方式進行處理,意味著工作線程一次只處理一個任務。而且當所有任務都完成之后,會自動停止Service,不需要我們手動停止。
IntentService 的使用
我們定義DownloadService類并繼承至IntentService。來模擬網絡下載的過程。public class DownloadService extends IntentService {private static int count = 0;/*** 主要用于調用服務類構造器** @param name 用于區分不同任務*/public DownloadService(String name) {super(name);}/*** AndroidManifest.xml配置清單需要配置** @param*/public
DownloadService() {super(
"action");}/***主要重寫該方法,在該方法內進行異步操作。**/@Overrideprotected void onHandleIntent(Intent intent) {Log.i(
"Download",
"onHandleIntent" + count);count++;String name = intent.getStringExtra(
"action");
if (name.equals(
"download")) {
for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();
return;}Log.i(
"Download",
"download:" + count);}}}//以下方法的重寫,僅僅為了打印日志@Overridepublic void
onDestroy() {super.onDestroy();Log.i(
"Download",
"onDestroy");}@Overridepublic void
onCreate() {super.onCreate();Log.i(
"Download",
"onCreate");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Log.i(
"Download",
"onStart");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(
"Download",
"onStartCommand");
return super.onStartCommand(intent, flags, startId);}
}
復制代碼在AndroidManifest.xml配置DownloadService。<service android:name=
".DownloadService"/>
復制代碼在MainActivity類中循環調用Service,啟動多循環任務。 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);Intent intent=new Intent(this,DownloadService.class);
for (int i =0;i<3;i++){intent.putExtra(
"action",
"download");intent.putExtra(
"count",
""+i);startService(intent);}}
復制代碼運行結果 分析: 從運行結果知道,IntentService在運行多個任務情況下,只調用一次onCreate,調用多次onStartCommand,跟Service的生命周期一致。但,只有在運行完download:1之后才會去運行download:2,接著是download:3。最后所有任務結束后會自動調用onDestroy,停止服務。在這里需要注意的是,和Service并不同,Service需要我們手動停止服務。對于結果的回調,可以采用接口回調,廣播,EventBus。
那么,IntentService是如何在Service中實現異步操作和串行處理任務的呢?
IntentService內部實現
查看IntentService的onCreate方法 @Overridepublic void
onCreate() {super.onCreate();//分析一HandlerThread thread = new HandlerThread(
"IntentService[" + mName +
"]");thread.start();mServiceLooper = thread.getLooper();分析二mServiceHandler = new ServiceHandler(mServiceLooper);}
復制代碼分析一
HandThread繼承Thread,通過start方法創建工作線程,內部建立Looper來達到消息循環,通過Hanlder消息機制來達到串行的效果和處理多任務。HandThread和Handler消息機制,可以另外查看文章。
分析二
ServiceHandler繼承Handler,與普通的Handler并沒有區別,在其內容處理handleMessage。即調用IntentService的onHandleIntent;
private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {onHandleIntent((Intent)msg.obj);stopSelf(msg.arg1);}}
復制代碼那么,當我們在Activity中重復調用startService方法時,只會多次調用onStartCommand方法,并不會重復調用onCreate方法。我們看看onStartComamnd方法的實現。 @Overridepublic int onStartCommand(@Nullable Intent intent, int flags, int startId) {onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}
復制代碼可以看得出,調用了onStart方法了。而onStart方法只是將我們的Intent包裝成Message,通過Handler發送出去,并在Handler中進行處理,調用我們的onHandleIntent。進而調用我們實現onHandleIntent的代碼。
@Overridepublic void onStart(@Nullable Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg);}
復制代碼總結
IntentService并沒有什么的新的技術含量,在了解HandlerTread和Handler的消息機制下,對Handler+Thread+Service作一個封裝,更適合我們后臺進行異步耗時操作的場景。有效避免通過new多個Thread。
知識點分享
Android消息機制
HandlerThread必知必會
如果覺得文章有用,給文章點個贊,鐵子
總結
以上是生活随笔為你收集整理的Android:IntentService的学习的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。