I am running a few tasks using a thread pool in a started foreground service. And, specifically, the service never calls stopSelf()
until the tasks are finished. I am wondering if it is necessary to interrupt/shutdown the thread pool in the onDestroy()
method. Will the OS ever forcefully destroy the service and call onDestroy()
? I know the OS can "kill" the service by killing the process but does it ever "kill" the service by simply destroying it.
Extra details: the service is not exported so external code cannot call Context.stopService()
and none of my own code calls Context.stopService()
, only Service.stopSelf()
.
CodePudding user response:
In principle, before Android terminates the process due to low memory conditions, it is supposed to call onDestroy()
on all running components, assuming that there is adequate time to do so. In practice, I haven't tested this scenario in ages.
Also, older versions of Android used to allow users to stop services through a Settings screen, and there is nothing stopping device manufacturers from offering a similar capability today.
I generally try to clean things up in onDestroy()
, just to be safe.