0

As the title suggests I would like to know when an android app was force killed by the operating system.
I need that for custom logs purpose and to try to find a bug in my app.
My app is wake upped with a notification from FCM service, but sometimes seems that notification is not received from the phone and so I cannot wake the app.

Any suggestions for this issue?
Thank you very much to all

skytron87
  • 91
  • 1
  • 10
  • 3
    Does this answer your question? [How to detect if android app is force stopped or uninstalled?](https://stackoverflow.com/questions/16013578/how-to-detect-if-android-app-is-force-stopped-or-uninstalled) –  Feb 25 '21 at 13:42
  • Thanks for the answer @NeutralMe – skytron87 Feb 25 '21 at 14:03
  • You are always welcome :-) @skytron87 –  Mar 12 '21 at 11:29

2 Answers2

2

Android does not officially expose such API. As mentioned in the referenced answer in the comments, the app process is being killed without leaving any trace.

However, you can persist some sort of a keep-alive watermark. Immediately after your app is created, update the watermark every X minutes/seconds (don’t use work manager periodic work since it wakes up the app, which beat the purpose).

Once your app is revived after being force-stopped, you can check when you app was alive and determine if it was killed.

The more frequently you update the watermark, the more accurate this workaround will be

Shlomi Katriel
  • 2,103
  • 1
  • 7
  • 20
  • 1
    Anytime :) In the app I work on, I also watch onDestroy calls so I could distinguish between force-stop and death by natural causes. – Shlomi Katriel Feb 25 '21 at 14:18
  • Thanks for the answer, it might be a good workaround for my problem. Suggest using a job scheduler for this job? @ShlomiKatriel – skytron87 Feb 25 '21 at 14:19
  • 1
    Nope. Actually work manager uses job scheduler on Android 8+ devices. Also, both are limited by minimum of 15 minutes of interval. I think alarm manager is best since it stops working if your app is killed – Shlomi Katriel Feb 25 '21 at 14:20
  • 1
    Ok, so can I use an AlarmManager? – skytron87 Feb 25 '21 at 14:24
  • 1
    Alarm Manager is great since repeating alarm have the same lifecycle as your app. Little example: https://codeplayon.medium.com/how-to-use-alarm-manager-and-broadcast-receiver-in-android-aa91415be8e3 – Shlomi Katriel Feb 25 '21 at 15:40
  • 1
    Thanks. Your advice was very helpful. – skytron87 Feb 26 '21 at 08:07
  • You're welcome. Please mark my answer as accepted (of course only if it indeed answered your question :)). – Shlomi Katriel Feb 26 '21 at 19:36