androidbackground-process

Detect Android background app shutdown and reason


I have an Android app that needs to persist in the background. The implementation--using receivers for some system intents to trigger actions (e.g. WAP_PUSH_RECEIVED), and a persistent notification--has been successful and working fine for many years.

But since Android 14, the app is shutdown (after many hours to few days) and the notification also disappears. (As of Android 13 persistent notifications can be swiped away as well, but we're keeping it as-is in our testing.)

I have not been using android:foregroundServiceType (introduced Android 10, but mandatory since Android 14?).

Anyway, after all that "background", my question is whether it's possible for an app to detect its being shutdown (similar to SIGTERM) and get a reason code. This would at least help me debug this issue better.

Thank you.


Solution

  • my question is whether it's possible for an app to detect its being shutdown (similar to SIGTERM) and get a reason code

    No, sorry.

    This would at least help me debug this issue better.

    On newer versions of Android, you can try using getHistoricalProcessExitReasons() on ActivityManager. This will tell you why a previous invocation of your app's process was terminated. See this section of this free book of mine for more on how to use this.