问题
I need to detect application process kill to run some clean up tasks on server side.
The solutions which is mentioned on popular questions are no longer works on Android 8 or later. Is there a way to detect application kill on Android 8 or later?
Pre-Android 8 solutions:
- How to know when my app has been killed?
- How to handle code when app is killed by swiping in android?
回答1:
You could use same solutions you linked, but starting from Oreo a SERVICE should be started as FOREGROUND_SERVICE or it will be killed withing 5 seconds from the System. So you can reuse 90% of previous code but you need to do changes according to new Oreo requirements.
However if the App is killed from "Android Settings --> Apps" page by selecting the "Terminate" button, even all Background Services are killed in the same time, so if this case occurs those methods will not work anymore.
来源:https://stackoverflow.com/questions/54017641/how-can-i-detect-app-kill-on-android-8-0-oreo-api-26-and-after