日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

IntentService的原理及應(yīng)用

來(lái)源: 責(zé)編: 時(shí)間:2024-05-09 09:24:53 140觀看
導(dǎo)讀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ì)象

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)停止。VDp28資訊網(wǎng)——每日最新資訊28at.com

與傳統(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ù)雜性。VDp28資訊網(wǎng)——每日最新資訊28at.com

IntentService使用

  1. 「創(chuàng)建 IntentService 子類(lèi)」

創(chuàng)建一個(gè)繼承自 IntentService 的類(lèi),重寫(xiě) onHandleIntent 方法。onHandleIntent 方法會(huì)在一個(gè)單獨(dú)的工作線(xiàn)程中運(yùn)行,用于處理你的后臺(tái)任務(wù)。VDp28資訊網(wǎng)——每日最新資訊28at.com

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)停止");    }}
  1. 「啟動(dòng) IntentService」

通過(guò)創(chuàng)建一個(gè) Intent 對(duì)象并調(diào)用 Context.startService(Intent) 方法來(lái)啟動(dòng) IntentService。你可以將需要傳遞給 IntentService 的數(shù)據(jù)放在 Intent 的 extras 中。VDp28資訊網(wǎng)——每日最新資訊28at.com

Intent intent = new Intent(MainActivity.this, ChildIntentService.class);intent.putExtra("serviceName", "ChildIntentService");startService(intent);
  1. 「停止 IntentService」通常不需要手動(dòng)停止 IntentService,因?yàn)槿蝿?wù)都處理完成后自動(dòng)停止。如果確實(shí)需要立即停止,可以調(diào)用 stopSelf() 方法。注意,即使調(diào)用了 stopSelf(),onHandleIntent 方法中正在處理的任務(wù)仍然會(huì)完成。
  2. 「處理結(jié)果」IntentService 在一個(gè)后臺(tái)線(xiàn)程中運(yùn)行,不能直接在 onHandleIntent 方法中更新 UI。如果需要將結(jié)果返回給 UI 線(xiàn)程,可以使用 Handler、BroadcastReceiver、LiveData、RxJava 等機(jī)制來(lái)實(shí)現(xiàn)。
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原理

IntentService封裝了HandlerThread和Handler,當(dāng)?shù)谝淮伪粏?dòng),會(huì)調(diào)用它的onCreate方法。VDp28資訊網(wǎng)——每日最新資訊28at.com

@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í)行。VDp28資訊網(wǎng)——每日最新資訊28at.com

每次啟動(dòng)IntentService,它的onStartCommand方法都會(huì)調(diào)用一次。VDp28資訊網(wǎng)——每日最新資訊28at.com

@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ā)送出去。VDp28資訊網(wǎng)——每日最新資訊28at.com

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ù)。VDp28資訊網(wǎng)——每日最新資訊28at.com

現(xiàn)在我們知道了,IntentService的內(nèi)部是通過(guò)消息的方式請(qǐng)求HandlerThread執(zhí)行任務(wù),HandlerThread內(nèi)部又是一種使用Handler的Thread,這就意味著IntentService和Looper一樣是順序執(zhí)行后臺(tái)任務(wù)的。VDp28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接: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

上一篇: 新手必看:Python中的字符串格式化入門(mén)指南

下一篇: 工作中最常見(jiàn)的6種OOM(內(nèi)存溢出)問(wèn)題,你知道幾個(gè)?

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top 主站蜘蛛池模板: 天气| 阿克| 刚察县| 嘉善县| 密云县| 泌阳县| 迁安市| 汕尾市| 枝江市| 泗水县| 杭锦后旗| 桦川县| 衡水市| 大荔县| 简阳市| 南昌市| 巢湖市| 佛教| 淳化县| 舟山市| 霍州市| 林州市| 台北市| 古田县| 来宾市| 桓仁| 乐清市| 分宜县| 巴南区| 盐津县| 论坛| 桓仁| 康定县| 高安市| 积石山| 镇平县| 康平县| 西盟| 苍溪县| 利川市| 周至县|