Android之IntentService源碼分析(第二篇:使用)

a、extends IntentService,重寫onHandleIntent方法,在裏面加入你自己的業務邏輯

b、startService(傳入Intent對象)

 

0、一個參數,接受一個Intent對象

    protected abstract void onHandleIntent(@Nullable Intent intent);

根據不同的業務,傳入不同狀態的Intent對象,這樣在該方法內部就能區分要進行的不同耗時工作了

 

1、三個參數,接受一個Intent對象、一個int、一個int

    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

startService調用後,onStartCommand方法就會被回調

第一個Intent對象:在startService中傳入的Intent對象

第二個flags則代表:Additional data about this start request.

第三個startId則代表:A unique integer representing this specific request to start. Use with {@link #stopSelfResult(int)}.

可見startId則是stopSelfResult方法就用到的,官方也說明你去看stopSelf方法就對了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章