Custom JobIntentService onHandleWork not called

前端 未结 12 1251
-上瘾入骨i
-上瘾入骨i 2020-12-09 08:27

I recently was updating an app that I work on to handle notifications from push using a JobIntentService instead of a regular IntentService because

12条回答
  •  旧时难觅i
    2020-12-09 08:43

    I encountered a somewhat similar problem with onHandleWork not being called the second time after migrating from Service to JobIntentService. Logs were showing that enqueueWork was called but onHandleWork was executing only the first and appeared to be stuck.

    After some more digging and logging, I discovered that the difference was that in a "stuck" scenario there was JobIntentService#onDestroy even though all operations in onHandleWork were performed and seemingly finished.

    Turned out that the culprit was bindService call of that service to activity lifecycle which was preventing disposing of the first job and for some reason calling enqueueWork after this condition was causing the service to "stuck" and never run any of the following onHandleWork again.

    So, here is an incorrect log of events in which JobIntentService will appear to be stuck after the first call never triggering onHandleWork again:

    enqueueWork -> first call
    onHandleWork started (log in the first line)
    onHandleWork finished (log in the last line)
    enqueueWork -> second call
    enqueueWork -> third call
    

    And here is the correct log of events with JobIntentService functioning correctly after removing bindService call:

    enqueueWork -> first call
    onHandleWork started (log in the first line)
    onHandleWork finished (log in the last line)
    onDestroy (service is destroyed after the job is finished)
    enqueueWork -> second call
    onHandleWork started (log in the first line)
    onHandleWork finished (log in the last line)
    onDestroy
    enqueueWork -> third call
    onHandleWork started (log in the first line)
    onHandleWork finished (log in the last line)
    onDestroy
    

    Hope this will be helpful to someone.

提交回复
热议问题