AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

IntentService的原理及應用

來源: 責編: 時間:2024-05-09 09:24:53 139觀看
導讀IntentService是Android中Service的一個子類,一般用于執行后臺耗時任務和處理異步請求。通過startService(Intent)方法傳遞請求給IntentService時,IntentService會在一個新的工作線程(worker thread)中處理每個Intent對象

IntentService是Android中Service的一個子類,一般用于執行后臺耗時任務和處理異步請求。通過startService(Intent)方法傳遞請求給IntentService時,IntentService會在一個新的工作線程(worker thread)中處理每個Intent對象。當所有的工作任務都執行完畢,IntentService會自動停止。iTp28資訊網——每日最新資訊28at.com

與傳統的Service不同,IntentService默認不會在主線程中運行,可以避免因執行耗時或可能被阻塞的操作而導致應用程序被掛起或出現ANR錯誤。IntentService內部創建了一個工作隊列(worker queue),一次只傳遞一個Intent到onHandleIntent方法中進行處理,簡化了多線程編程的復雜性。iTp28資訊網——每日最新資訊28at.com

IntentService使用

  1. 「創建 IntentService 子類」

創建一個繼承自 IntentService 的類,重寫 onHandleIntent 方法。onHandleIntent 方法會在一個單獨的工作線程中運行,用于處理你的后臺任務。iTp28資訊網——每日最新資訊28at.com

public class ChildIntentService extends IntentService {    public ChildIntentService() {        super("Reathin");    }    @Override    protected void onHandleIntent(@Nullable Intent intent) {        //執行耗時任務        Log.d(TAG, "onHandleIntent:耗時任務開始");        String serviceName = intent.getStringExtra("serviceName");        if (TextUtils.equals(serviceName, "ChildIntentService")){            simulationTask();            Log.d(TAG, "onHandleIntent:耗時任務完成");        }    }    /**     * 模擬耗時任務     */    private void simulationTask() {        try {            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    @Override    public void onDestroy() {        super.onDestroy();        Log.d(TAG, "onDestroy:服務自動停止");    }}
  1. 「啟動 IntentService」

通過創建一個 Intent 對象并調用 Context.startService(Intent) 方法來啟動 IntentService。你可以將需要傳遞給 IntentService 的數據放在 Intent 的 extras 中。iTp28資訊網——每日最新資訊28at.com

Intent intent = new Intent(MainActivity.this, ChildIntentService.class);intent.putExtra("serviceName", "ChildIntentService");startService(intent);
  1. 「停止 IntentService」通常不需要手動停止 IntentService,因為任務都處理完成后自動停止。如果確實需要立即停止,可以調用 stopSelf() 方法。注意,即使調用了 stopSelf(),onHandleIntent 方法中正在處理的任務仍然會完成。
  2. 「處理結果」IntentService 在一個后臺線程中運行,不能直接在 onHandleIntent 方法中更新 UI。如果需要將結果返回給 UI 線程,可以使用 Handler、BroadcastReceiver、LiveData、RxJava 等機制來實現。
2024-05-07 18:05:41.712 11300-11349 onHandleIntent:耗時任務開始2024-05-07 18:05:46.713 11300-11349 onHandleIntent:耗時任務完成2024-05-07 18:05:46.716 11300-11300 onDestroy:服務自動停止

IntentService原理

IntentService封裝了HandlerThread和Handler,當第一次被啟動,會調用它的onCreate方法。iTp28資訊網——每日最新資訊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方法會創建一個HandlerThread對象并調用它的start方法,利用這個HandlerThread的Looper創建ServiceHandler對象mServiceHandler,這樣通過mServiceHandler發送的消息最終都會在HandlerThread中執行。iTp28資訊網——每日最新資訊28at.com

每次啟動IntentService,它的onStartCommand方法都會調用一次。iTp28資訊網——每日最新資訊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方法中直接調用onStart方法,onStart方法只是把intent封裝進一個消息,并通過mServiceHandler發送出去。iTp28資訊網——每日最新資訊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內部很簡單,在收到消息之后會把消息傳遞給onHandleIntent方法處理,onHandleIntent方法需要我們在子類中實現,它的作用是通過Intent區分具體任務并執行這些任務。當onHandleIntent方法結束后會調用IntentService的stopSelf(int startId)方法嘗試停止服務,因為這個時候可能還有其他消息未處理,只有所有消息都處理完才會真的停止服務。iTp28資訊網——每日最新資訊28at.com

現在我們知道了,IntentService的內部是通過消息的方式請求HandlerThread執行任務,HandlerThread內部又是一種使用Handler的Thread,這就意味著IntentService和Looper一樣是順序執行后臺任務的。iTp28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-87486-0.htmlIntentService的原理及應用

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

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

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

標簽:
  • 熱門焦點
Top