日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Service学习之IntentService 深入分析

發布時間:2024/1/17 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Service学习之IntentService 深入分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是IntentService? (本文轉自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對象,在適當的時候會停止自己(一般在工作完成的時候). 所有的請求的處理都在一個工作線程中完成,它們會交替執行(但不會阻塞主線程的執行),一次只能執行一個請求.(**本人修改了原文的一些翻譯) 下面是要分析的源碼: 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 的集合體,他不僅有服務的功能,還有處理和循環消息的功能.
下面是onCreate()的源碼: @Override ??????? public void onCreate() { ??????????????? super.onCreate();
??????????????? HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); ??????????????? thread.start();
??????????????? mServiceLooper = thread.getLooper(); ??????????????? mServiceHandler = new ServiceHandler(mServiceLooper); ??????? } 分析:IntentService創建時就會創建Handler線程(HandlerThread)并且啟動,然后再得到當前線程的Looper對象來初始化IntentService的mServiceLooper,接著創建mServicehandler對象. 下面是onStart()的源碼: @Override ??????? public void onStart(Intent intent, int startId) { ??????????????? Message msg = mServiceHandler.obtainMessage(); ??????????????? msg.arg1 = startId; ??????????????? msg.obj = intent;
??????????????? mServiceHandler.sendMessage(msg); ??????? } 分析:當你啟動IntentService的時候,就會產生一條附帶startId和Intent的Message并發送到MessageQueue中,接下來Looper發現MessageQueue中有Message的時候,就會停止Handler處理消息,接下來處理的代碼如下: @Override ??????? public void handleMessage(Message msg) { ??????????????????????? onHandleIntent((Intent)msg.obj); ??????????????????????? stopSelf(msg.arg1); ??????? } 接著調用 onHandleIntent((Intent)msg.obj),這是一個抽象的方法,其實就是我們要重寫實現的方法,我們可以在這個方法里面處理我們的工作.當任務完成時就會調用stopSelf(msg.arg1)這個方法來結束指定的工作. 當所有的工作執行完后:就會執行onDestroy方法,源碼如下: @Override ??????? public void onDestroy() { ??????????????? mServiceLooper.quit(); ??????? } 服務結束后調用這個方法 mServiceLooper.quit()使looper停下來.
通過對源碼的分析得出: ??? 這是一個基于消息的服務,每次啟動該服務并不是馬上處理你的工作,而是首先會創建對應的Looper,Handler并且在MessageQueue中添加的附帶客戶Intent的Message對象,當Looper發現有Message的時候接著得到Intent對象通過在onHandleIntent((Intent)msg.obj)中調用你的處理程序.處理完后即會停止自己的服務.意思是Intent的生命周期跟你的處理的任務是一致的.所以這個類用下載任務中非常好,下載任務結束后服務自身就會結束退出

轉載于:https://www.cnblogs.com/senior-engineer/p/4492312.html

總結

以上是生活随笔為你收集整理的Android Service学习之IntentService 深入分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。