IntentService與Service的區(qū)別

IntentService是繼承并處理異步請(qǐng)求的一個(gè)類(lèi),在IntentService內(nèi)有一個(gè)工作線程來(lái)處理耗時(shí)操作,啟動(dòng)IntentService的方式和啟動(dòng)傳統(tǒng)的Service一樣,同時(shí),當(dāng)任務(wù)執(zhí)行完后,IntentService會(huì)自動(dòng)停止,而不需要我們手動(dòng)去控制或stopSelf()。另外,可以啟動(dòng)IntentService多次,而每一個(gè)耗時(shí)操作會(huì)以工作隊(duì)列的方式在IntentService的onHandleIntent回調(diào)方法中執(zhí)行,并且,每次只會(huì)執(zhí)行一個(gè)工作線程,執(zhí)行完第一個(gè)再執(zhí)行第二個(gè),以此類(lèi)推。


先來(lái)看一下IntentService類(lèi)的源碼:

publicvoid 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 =newHandlerThread("IntentService[" + mName + "]");

? ? ? ? thread.start(); //開(kāi)啟一個(gè)工作線程? ? ? ? mServiceLooper = thread.getLooper();//單獨(dú)的消息隊(duì)列mServiceHandler =new ServiceHandler(mServiceLooper);

}



定義一個(gè)IntentService的子類(lèi):

publicclassMIntentServiceextends IntentService {

? ? public MIntentService(){

? ? ? ? super("MIntentService");

? ? }

? ? /**? ? * Creates an IntentService.? Invoked by your subclass's constructor.

? ? * @param name Used to name the worker thread, important only for debugging.

? ? */public MIntentService(String name) {

? ? ? ? super(name);

? ? }

? ? @Override

? ? publicvoid onCreate() {

? ? ? ? Log.e("MIntentService--", "onCreate");

? ? ? ? super.onCreate();

? ? }

? ? @Override

? ? publicintonStartCommand(Intent intent,intflags,int startId) {

? ? ? ? Log.e("MIntentService--", "onStartCommand");

? ? ? ? returnsuper.onStartCommand(intent, flags, startId);

? ? }

? ? @Override

? ? protectedvoid onHandleIntent(Intent intent) {

? ? ? ? Log.e("MIntentService--", Thread.currentThread().getName() + "--" + intent.getStringExtra("info") );

? ? ? ? for(inti = 0; i < 100; i++){//耗時(shí)操作Log.i("onHandleIntent--",? i + "--" + Thread.currentThread().getName());

? ? ? ? }

? ? }

? ? @Override

? ? publicvoid onDestroy() {

? ? ? ? Log.e("MIntentService--", "onDestroy");

? ? ? ? super.onDestroy();

? ? }

}


開(kāi)啟IntentService服務(wù):

publicvoid intentClick(View v){

? ? ? ? Intent intent =newIntent(this, MIntentService.class);

? ? ? ? intent.putExtra("info", "good good study");

? ? ? ? startService(intent);

}


點(diǎn)擊按鈕之后輸出結(jié)果為(過(guò)濾log.e):

10-25 16:54:58.852? 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onCreate10-25 16:54:58.852? 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onStartCommand10-25 16:54:58.856? 27135-27354/com.example.lenovo.myintentservicedemo E/MIntentService--﹕IntentService[MIntentService]--good good study10-25 16:54:58.879? 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onDestroy

 Intent服務(wù)開(kāi)啟后,執(zhí)行完onHandleIntent里面的任務(wù)就自動(dòng)銷(xiāo)毀結(jié)束,通過(guò)打印的線程名稱(chēng)可以發(fā)現(xiàn)是新開(kāi)了一個(gè)線程來(lái)處理耗時(shí)操作的,即是耗時(shí)操作也可以被這個(gè)線程管理和執(zhí)行,同時(shí)不會(huì)產(chǎn)生ANR的情況。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容