IntentService是Android中Service的一個(gè)子類(lèi),一般用于執(zhí)行后臺(tái)耗時(shí)任務(wù)和處理異步請(qǐng)求。通過(guò)startService(Intent)方法傳遞請(qǐng)求給IntentService時(shí),IntentService會(huì)在一個(gè)新的工作線(xiàn)程(worker thread)中處理每個(gè)Intent對(duì)象。當(dāng)所有的工作任務(wù)都執(zhí)行完畢,IntentService會(huì)自動(dòng)停止。
與傳統(tǒng)的Service不同,IntentService默認(rèn)不會(huì)在主線(xiàn)程中運(yùn)行,可以避免因執(zhí)行耗時(shí)或可能被阻塞的操作而導(dǎo)致應(yīng)用程序被掛起或出現(xiàn)ANR錯(cuò)誤。IntentService內(nèi)部創(chuàng)建了一個(gè)工作隊(duì)列(worker queue),一次只傳遞一個(gè)Intent到onHandleIntent方法中進(jìn)行處理,簡(jiǎn)化了多線(xiàn)程編程的復(fù)雜性。
創(chuàng)建一個(gè)繼承自 IntentService 的類(lèi),重寫(xiě) onHandleIntent 方法。onHandleIntent 方法會(huì)在一個(gè)單獨(dú)的工作線(xiàn)程中運(yùn)行,用于處理你的后臺(tái)任務(wù)。
public class ChildIntentService extends IntentService { public ChildIntentService() { super("Reathin"); } @Override protected void onHandleIntent(@Nullable Intent intent) { //執(zhí)行耗時(shí)任務(wù) Log.d(TAG, "onHandleIntent:耗時(shí)任務(wù)開(kāi)始"); String serviceName = intent.getStringExtra("serviceName"); if (TextUtils.equals(serviceName, "ChildIntentService")){ simulationTask(); Log.d(TAG, "onHandleIntent:耗時(shí)任務(wù)完成"); } } /** * 模擬耗時(shí)任務(wù) */ private void simulationTask() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy:服務(wù)自動(dòng)停止"); }}
通過(guò)創(chuàng)建一個(gè) Intent 對(duì)象并調(diào)用 Context.startService(Intent) 方法來(lái)啟動(dòng) IntentService。你可以將需要傳遞給 IntentService 的數(shù)據(jù)放在 Intent 的 extras 中。
Intent intent = new Intent(MainActivity.this, ChildIntentService.class);intent.putExtra("serviceName", "ChildIntentService");startService(intent);
2024-05-07 18:05:41.712 11300-11349 onHandleIntent:耗時(shí)任務(wù)開(kāi)始2024-05-07 18:05:46.713 11300-11349 onHandleIntent:耗時(shí)任務(wù)完成2024-05-07 18:05:46.716 11300-11300 onDestroy:服務(wù)自動(dòng)停止
IntentService封裝了HandlerThread和Handler,當(dāng)?shù)谝淮伪粏?dòng),會(huì)調(diào)用它的onCreate方法。
@Overridepublic 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);}
onCreate方法會(huì)創(chuàng)建一個(gè)HandlerThread對(duì)象并調(diào)用它的start方法,利用這個(gè)HandlerThread的Looper創(chuàng)建ServiceHandler對(duì)象mServiceHandler,這樣通過(guò)mServiceHandler發(fā)送的消息最終都會(huì)在HandlerThread中執(zhí)行。
每次啟動(dòng)IntentService,它的onStartCommand方法都會(huì)調(diào)用一次。
@Overridepublic void onStart(@Nullable Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg);}@Overridepublic int onStartCommand(@Nullable Intent intent, int flags, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}
onStartCommand方法中直接調(diào)用onStart方法,onStart方法只是把intent封裝進(jìn)一個(gè)消息,并通過(guò)mServiceHandler發(fā)送出去。
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); }}
ServiceHandler內(nèi)部很簡(jiǎn)單,在收到消息之后會(huì)把消息傳遞給onHandleIntent方法處理,onHandleIntent方法需要我們?cè)谧宇?lèi)中實(shí)現(xiàn),它的作用是通過(guò)Intent區(qū)分具體任務(wù)并執(zhí)行這些任務(wù)。當(dāng)onHandleIntent方法結(jié)束后會(huì)調(diào)用IntentService的stopSelf(int startId)方法嘗試停止服務(wù),因?yàn)檫@個(gè)時(shí)候可能還有其他消息未處理,只有所有消息都處理完才會(huì)真的停止服務(wù)。
現(xiàn)在我們知道了,IntentService的內(nèi)部是通過(guò)消息的方式請(qǐng)求HandlerThread執(zhí)行任務(wù),HandlerThread內(nèi)部又是一種使用Handler的Thread,這就意味著IntentService和Looper一樣是順序執(zhí)行后臺(tái)任務(wù)的。
本文鏈接:http://www.www897cc.com/showinfo-26-87486-0.htmlIntentService的原理及應(yīng)用
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com