深入剖析Android系統(tǒng)中Service和IntentService的區(qū)別
Android中的Service是用于后臺(tái)服務(wù)的,當(dāng)應(yīng)用程序被掛到后臺(tái)的時(shí)候,問了保證應(yīng)用某些組件仍然可以工作而引入了Service這個(gè)概念,那么這里面要強(qiáng)調(diào)的是Service不是獨(dú)立的進(jìn)程,也不是獨(dú)立的線程,它是依賴于應(yīng)用程序的主線程的,也就是說,在更多時(shí)候不建議在Service中編寫耗時(shí)的邏輯和操作,否則會(huì)引起ANR。
那么我們當(dāng)我們編寫的耗時(shí)邏輯,不得不被service來管理的時(shí)候,就需要引入IntentService,IntentService是繼承Service的,那么它包含了Service的全部特性,當(dāng)然也包含service的生命周期,那么與service不同的是,IntentService在執(zhí)行onCreate操作的時(shí)候,內(nèi)部開了一個(gè)線程,去你執(zhí)行你的耗時(shí)操作。
service本身存在兩個(gè)問題:
(1)service不會(huì)專門啟動(dòng)一條單獨(dú)的進(jìn)程,service與它所在的應(yīng)用位于同一個(gè)進(jìn)程。
(2)service也不是專門新的一條線程,不應(yīng)該在service中處理耗時(shí)的操作。
IntentService很好的彌補(bǔ)了這一點(diǎn):
(1)IntentService會(huì)創(chuàng)建單獨(dú)的worker線程來處理所有的intent請(qǐng)求。
(2)IntentService會(huì)創(chuàng)建單獨(dú)的worker線程來處理onHandleIntent()方法實(shí)現(xiàn)的代碼。
(3)當(dāng)所有的請(qǐng)求處理完之后,IntentService會(huì)自動(dòng)停止。
(4)為Service的OnBind()方法提供了默認(rèn)的實(shí)現(xiàn),返回null。
(5)為service的onStartCommand()方法提供了默認(rèn)的實(shí)現(xiàn),該實(shí)現(xiàn)會(huì)將請(qǐng)求intent添加到隊(duì)列中。
所以對(duì)IntentService的使用就是:繼承IntentService,重寫onHandleIntent()方法即可。
tips:
(1)Intentservice也必須在manifest中聲明。
(2)實(shí)現(xiàn)類的構(gòu)造方法必須實(shí)現(xiàn)默認(rèn)的構(gòu)造方法。
這里我 需要解釋以下幾個(gè)方法,也許大家都已經(jīng)很清楚了,不過為了拋磚引玉,我還是要提一嘴。
Service中提供了一個(gè)方法:
public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY; }
這個(gè)方法的具體含義是,當(dāng)你的需要這個(gè)service啟動(dòng)的時(shí)候,或者調(diào)用這個(gè)servcie的時(shí)候,那么這個(gè)方法首先是要被回調(diào)的。
同時(shí)IntentService中提供了這么一個(gè)方法:
protected abstract void onHandleIntent(Intent intent);
這是一個(gè)抽象方法,也就是說具體的實(shí)現(xiàn)需要被延伸到子類。
子類的聲明:
public class ChargeService extends IntentService
上面提到過IntentService是繼承Service的,那么這個(gè)子類也肯定繼承service,那么onHandleIntent()方法是什么時(shí)候被調(diào)用的呢?讓我們具體看IntentService的內(nèi)部實(shí)現(xiàn):
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); } } /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public IntentService(String name) { super(); mName = name; } /** * Sets intent redelivery preferences. Usually called from the constructor * with your preferred semantics. * * <p>If enabled is true, * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_REDELIVER_INTENT}, so if this process dies before * {@link #onHandleIntent(Intent)} returns, the process will be restarted * and the intent redelivered. If multiple Intents have been sent, only * the most recent one is guaranteed to be redelivered. * * <p>If enabled is false (the default), * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent * dies along with it. */ public void setIntentRedelivery(boolean enabled) { mRedelivery = enabled; } @Override public void onCreate() { // TODO: It would be nice to have an option to hold a partial wakelock // during processing, and to have a static startService(Context, Intent) // method that would launch the service & hand off a wakelock. super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Override public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
在這里我們可以清楚的看到其實(shí)IntentService在執(zhí)行onCreate的方法的時(shí)候,其實(shí)開了一個(gè)線程HandlerThread,并獲得了當(dāng)前線程隊(duì)列管理的looper,并且在onStart的時(shí)候,把消息置入了消息隊(duì)列,
@Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); }
在消息被handler接受并且回調(diào)的時(shí)候,執(zhí)行了onHandlerIntent方法,該方法的實(shí)現(xiàn)是子類去做的。
結(jié)論:
IntentService是通過Handler looper message的方式實(shí)現(xiàn)了一個(gè)多線程的操作,同時(shí)耗時(shí)操作也可以被這個(gè)線程管理和執(zhí)行,同時(shí)不會(huì)產(chǎn)生ANR的情況。
相關(guān)文章
Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過本文學(xué)習(xí)吧2018-05-05android textview設(shè)置字體的行距和字間距
這篇文章主要介紹了android textview設(shè)置字體的行距和字間距的方法,非常簡單實(shí)用,有需要的小伙伴可以參考下2016-05-05Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫實(shí)例詳解
這篇文章主要介紹了Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android操作SQLite數(shù)據(jù)庫的基本技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)
這篇文章主要介紹了Android自定義控件案例匯總,自定義開關(guān)、Listview實(shí)現(xiàn)下拉刷新、側(cè)滑菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android實(shí)現(xiàn)購物車添加商品動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購物車添加商品動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android 7.0 SEAndroid app權(quán)限配置方法
今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07