Android Service学习之IntentService 深入分析
生活随笔
收集整理的這篇文章主要介紹了
Android Service学习之IntentService 深入分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
什么是IntentService? (本文轉(zhuǎn)自http://blog.csdn.net/gaojie314/archive/2010/11/28/6040701.aspx) 官方的解釋是: IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through android.content.Context.startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time. 意思是說:IntentService是一個通過Context.startService(Intent)啟動可以處理異步請求的Service,使用時你只需要繼承IntentService和重寫其中的onHandleIntent(Intent)方法接收一個Intent對象,在適當?shù)臅r候會停止自己(一般在工作完成的時候). 所有的請求的處理都在一個工作線程中完成,它們會交替執(zhí)行(但不會阻塞主線程的執(zhí)行),一次只能執(zhí)行一個請求.(**本人修改了原文的一些翻譯) 下面是要分析的源碼: 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); ??????????????? }
??????? } 從源碼可以分析出: IntentService 實際上是Looper,Handler,Service 的集合體,他不僅有服務(wù)的功能,還有處理和循環(huán)消息的功能.
下面是onCreate()的源碼: @Override ??????? public void onCreate() { ??????????????? super.onCreate();
??????????????? HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); ??????????????? thread.start();
??????????????? mServiceLooper = thread.getLooper(); ??????????????? mServiceHandler = new ServiceHandler(mServiceLooper); ??????? } 分析:IntentService創(chuàng)建時就會創(chuàng)建Handler線程(HandlerThread)并且啟動,然后再得到當前線程的Looper對象來初始化IntentService的mServiceLooper,接著創(chuàng)建mServicehandler對象. 下面是onStart()的源碼: @Override ??????? public void onStart(Intent intent, int startId) { ??????????????? Message msg = mServiceHandler.obtainMessage(); ??????????????? msg.arg1 = startId; ??????????????? msg.obj = intent;
??????????????? mServiceHandler.sendMessage(msg); ??????? } 分析:當你啟動IntentService的時候,就會產(chǎn)生一條附帶startId和Intent的Message并發(fā)送到MessageQueue中,接下來Looper發(fā)現(xiàn)MessageQueue中有Message的時候,就會停止Handler處理消息,接下來處理的代碼如下: @Override ??????? public void handleMessage(Message msg) { ??????????????????????? onHandleIntent((Intent)msg.obj); ??????????????????????? stopSelf(msg.arg1); ??????? } 接著調(diào)用 onHandleIntent((Intent)msg.obj),這是一個抽象的方法,其實就是我們要重寫實現(xiàn)的方法,我們可以在這個方法里面處理我們的工作.當任務(wù)完成時就會調(diào)用stopSelf(msg.arg1)這個方法來結(jié)束指定的工作. 當所有的工作執(zhí)行完后:就會執(zhí)行onDestroy方法,源碼如下: @Override ??????? public void onDestroy() { ??????????????? mServiceLooper.quit(); ??????? } 服務(wù)結(jié)束后調(diào)用這個方法 mServiceLooper.quit()使looper停下來.
通過對源碼的分析得出: ??? 這是一個基于消息的服務(wù),每次啟動該服務(wù)并不是馬上處理你的工作,而是首先會創(chuàng)建對應的Looper,Handler并且在MessageQueue中添加的附帶客戶Intent的Message對象,當Looper發(fā)現(xiàn)有Message的時候接著得到Intent對象通過在onHandleIntent((Intent)msg.obj)中調(diào)用你的處理程序.處理完后即會停止自己的服務(wù).意思是Intent的生命周期跟你的處理的任務(wù)是一致的.所以這個類用下載任務(wù)中非常好,下載任務(wù)結(jié)束后服務(wù)自身就會結(jié)束退出
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time. 意思是說:IntentService是一個通過Context.startService(Intent)啟動可以處理異步請求的Service,使用時你只需要繼承IntentService和重寫其中的onHandleIntent(Intent)方法接收一個Intent對象,在適當?shù)臅r候會停止自己(一般在工作完成的時候). 所有的請求的處理都在一個工作線程中完成,它們會交替執(zhí)行(但不會阻塞主線程的執(zhí)行),一次只能執(zhí)行一個請求.(**本人修改了原文的一些翻譯) 下面是要分析的源碼: 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); ??????????????? }
??????? } 從源碼可以分析出: IntentService 實際上是Looper,Handler,Service 的集合體,他不僅有服務(wù)的功能,還有處理和循環(huán)消息的功能.
下面是onCreate()的源碼: @Override ??????? public void onCreate() { ??????????????? super.onCreate();
??????????????? HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); ??????????????? thread.start();
??????????????? mServiceLooper = thread.getLooper(); ??????????????? mServiceHandler = new ServiceHandler(mServiceLooper); ??????? } 分析:IntentService創(chuàng)建時就會創(chuàng)建Handler線程(HandlerThread)并且啟動,然后再得到當前線程的Looper對象來初始化IntentService的mServiceLooper,接著創(chuàng)建mServicehandler對象. 下面是onStart()的源碼: @Override ??????? public void onStart(Intent intent, int startId) { ??????????????? Message msg = mServiceHandler.obtainMessage(); ??????????????? msg.arg1 = startId; ??????????????? msg.obj = intent;
??????????????? mServiceHandler.sendMessage(msg); ??????? } 分析:當你啟動IntentService的時候,就會產(chǎn)生一條附帶startId和Intent的Message并發(fā)送到MessageQueue中,接下來Looper發(fā)現(xiàn)MessageQueue中有Message的時候,就會停止Handler處理消息,接下來處理的代碼如下: @Override ??????? public void handleMessage(Message msg) { ??????????????????????? onHandleIntent((Intent)msg.obj); ??????????????????????? stopSelf(msg.arg1); ??????? } 接著調(diào)用 onHandleIntent((Intent)msg.obj),這是一個抽象的方法,其實就是我們要重寫實現(xiàn)的方法,我們可以在這個方法里面處理我們的工作.當任務(wù)完成時就會調(diào)用stopSelf(msg.arg1)這個方法來結(jié)束指定的工作. 當所有的工作執(zhí)行完后:就會執(zhí)行onDestroy方法,源碼如下: @Override ??????? public void onDestroy() { ??????????????? mServiceLooper.quit(); ??????? } 服務(wù)結(jié)束后調(diào)用這個方法 mServiceLooper.quit()使looper停下來.
通過對源碼的分析得出: ??? 這是一個基于消息的服務(wù),每次啟動該服務(wù)并不是馬上處理你的工作,而是首先會創(chuàng)建對應的Looper,Handler并且在MessageQueue中添加的附帶客戶Intent的Message對象,當Looper發(fā)現(xiàn)有Message的時候接著得到Intent對象通過在onHandleIntent((Intent)msg.obj)中調(diào)用你的處理程序.處理完后即會停止自己的服務(wù).意思是Intent的生命周期跟你的處理的任務(wù)是一致的.所以這個類用下載任務(wù)中非常好,下載任務(wù)結(jié)束后服務(wù)自身就會結(jié)束退出
轉(zhuǎn)載于:https://www.cnblogs.com/senior-engineer/p/4492312.html
總結(jié)
以上是生活随笔為你收集整理的Android Service学习之IntentService 深入分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模态视图
- 下一篇: JSP-05- JSP总结