0

On device with Android Oreo 8.0 (API level 26) JobService.onTaskRemoved() isn't called

  • How to listen for such an event?

Code used which stopped working:

class TaskRemovalWatcherService: JobIntentService() {
    ...
    override fun onTaskRemoved(rootIntent: Intent?) {  
    //not called on Android Oreo

        super.onTaskRemoved(rootIntent);   
        onAppRemovedFromRecentApps()
    }
}

When App-Task is removed from recent apps I need to

  1. Stop a process
  2. Finish 2nd AppTask

if AppTasks.size = 2 and 1 task is removed from recent apps, then process is NOT killed, because 2nd AppTask still in memory.

Update:

  1. when use JobIntentService.enqueueWork then right after JobIntentService.onHandleWork() JobIntentService.onDestroy() is called
  2. when use JobService.schedule() throws IllegalArgumentException when schadule job without constraints.

P.S. The idea of checking each X seconds isTaskWasRemoved seems not a good one.

StepanM
  • 4,222
  • 1
  • 21
  • 25

2 Answers2

0

You can override JobService#onStop() and then create a message and pass it to the other service, when the service receives that message you can clean up it's work and kill it.

Zachary Sweigart
  • 1,051
  • 9
  • 23
0

The expected behavior of START_STICKY might have changed due to custom implementation by device manufacturer or due to new implementation in android Oreo

You can use JobScheduler to monitor a service lifecycle after regular interval. If service is destroyed, then you can perform your task in onDestroy() method just before service is actually being destroyed.

This will remove your dependency from onTaskremoved() method.

Please refer below question with similar problem onTaskRemoved() not getting called when Home press & kill the app

ravidl
  • 1,055
  • 2
  • 9
  • 18