深入剖析Android系統(tǒng)中Service和IntentService的區(qū)別
Android中的Service是用于后臺服務(wù)的,當(dāng)應(yīng)用程序被掛到后臺的時候,問了保證應(yīng)用某些組件仍然可以工作而引入了Service這個概念,那么這里面要強調(diào)的是Service不是獨立的進程,也不是獨立的線程,它是依賴于應(yīng)用程序的主線程的,也就是說,在更多時候不建議在Service中編寫耗時的邏輯和操作,否則會引起ANR。
那么我們當(dāng)我們編寫的耗時邏輯,不得不被service來管理的時候,就需要引入IntentService,IntentService是繼承Service的,那么它包含了Service的全部特性,當(dāng)然也包含service的生命周期,那么與service不同的是,IntentService在執(zhí)行onCreate操作的時候,內(nèi)部開了一個線程,去你執(zhí)行你的耗時操作。
service本身存在兩個問題:
(1)service不會專門啟動一條單獨的進程,service與它所在的應(yīng)用位于同一個進程。
(2)service也不是專門新的一條線程,不應(yīng)該在service中處理耗時的操作。
IntentService很好的彌補了這一點:
(1)IntentService會創(chuàng)建單獨的worker線程來處理所有的intent請求。
(2)IntentService會創(chuàng)建單獨的worker線程來處理onHandleIntent()方法實現(xiàn)的代碼。
(3)當(dāng)所有的請求處理完之后,IntentService會自動停止。
(4)為Service的OnBind()方法提供了默認(rèn)的實現(xiàn),返回null。
(5)為service的onStartCommand()方法提供了默認(rèn)的實現(xiàn),該實現(xiàn)會將請求intent添加到隊列中。
所以對IntentService的使用就是:繼承IntentService,重寫onHandleIntent()方法即可。
tips:
(1)Intentservice也必須在manifest中聲明。
(2)實現(xiàn)類的構(gòu)造方法必須實現(xiàn)默認(rèn)的構(gòu)造方法。
這里我 需要解釋以下幾個方法,也許大家都已經(jīng)很清楚了,不過為了拋磚引玉,我還是要提一嘴。
Service中提供了一個方法:
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
這個方法的具體含義是,當(dāng)你的需要這個service啟動的時候,或者調(diào)用這個servcie的時候,那么這個方法首先是要被回調(diào)的。
同時IntentService中提供了這么一個方法:
protected abstract void onHandleIntent(Intent intent);
這是一個抽象方法,也就是說具體的實現(xiàn)需要被延伸到子類。
子類的聲明:
public class ChargeService extends IntentService
上面提到過IntentService是繼承Service的,那么這個子類也肯定繼承service,那么onHandleIntent()方法是什么時候被調(diào)用的呢?讓我們具體看IntentService的內(nèi)部實現(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);
}
在這里我們可以清楚的看到其實IntentService在執(zhí)行onCreate的方法的時候,其實開了一個線程HandlerThread,并獲得了當(dāng)前線程隊列管理的looper,并且在onStart的時候,把消息置入了消息隊列,
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
在消息被handler接受并且回調(diào)的時候,執(zhí)行了onHandlerIntent方法,該方法的實現(xiàn)是子類去做的。
結(jié)論:
IntentService是通過Handler looper message的方式實現(xiàn)了一個多線程的操作,同時耗時操作也可以被這個線程管理和執(zhí)行,同時不會產(chǎn)生ANR的情況。
相關(guān)文章
解決genymotion模擬器無法聯(lián)網(wǎng)的正確方法100%成功
android 5.1版不能聯(lián)網(wǎng),三個步驟的設(shè)置就可以解決你的genymotion模擬器無法聯(lián)網(wǎng)的問題2018-03-03
androidstudio3.0使用butterknife報錯解決的解決方法
這篇文章主要介紹了androidstudio3.0使用butterknife報錯解決的解決方法,非常具有實用價值,需要的朋友可以參考下2018-01-01
Android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)
這篇文章主要介紹了android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
如何判斷軟件程序是否聯(lián)網(wǎng) 聯(lián)網(wǎng)狀態(tài)提示信息Android實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何判斷軟件程序是否聯(lián)網(wǎng)的實現(xiàn)代碼,Android實現(xiàn)聯(lián)網(wǎng)狀態(tài)信息提示,感興趣的小伙伴們可以參考一下2016-05-05
Android DrawerLayout實現(xiàn)側(cè)拉菜單功能
這篇文章主要介紹了Android DrawerLayout實現(xiàn)側(cè)拉菜單功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Android使用ViewPager實現(xiàn)導(dǎo)航
本文主要介紹了Android使用ViewPager實現(xiàn)導(dǎo)航的方法代碼。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03

