Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often in Android 8 & above

前端 未结 3 762
野趣味
野趣味 2021-02-06 09:50

I have recently replaced all my service to foreground services and JobIntentService since there are some background execution limits (https://developer.android.com/about/version

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 10:36

    JobIntentService which acts like Intent Service is not getting stopped once the work is done

    The issue is in your extended class FixedJobIntentService dequeueWork method.

    Try changing it to something like below

    GenericWorkItem superValue = super.dequeueWork();
    if (superValue != null) {
        return new FixedGenericWorkItem(superValue);
    }
    return null;
    

    Looking at the JobIntentSerivce code, Work Items processor logic is below, i.e until there are no work items left in the queue all items are processed (i.e onHandleWork is called for each item)

        while ((work = dequeueWork()) != null) {
            if (DEBUG) Log.d(TAG, "Processing next work: " + work);
            onHandleWork(work.getIntent());
            if (DEBUG) Log.d(TAG, "Completing work: " + work);
            work.complete();
        }
    

    Issue in your implementation is after processing the first work item, the super.dequeueWork() returns null, which you are not taking care of and just sending a new FixedGenericWorkItem object passing null value. You might observe that a null value is passed to your onHandleWork in your subsequent calls.

    Hope this helps resolve your issue.

提交回复
热议问题