Search code examples
androidbroadcastreceiverandroid-broadcastreceiver

Prevent android app from entering force closed state


I have an Android app that must receive broadcast messages through a BroadcastReceiver. On my Huawei phone with Android 5.1 (api level 22), the messages are received only if the app is not in the state "force closed", i.e, when the screen with informations about the app contains an enabled force close button. If the app is in the force closed state, that button is grayed out. (phone screenshot )

The messages are received some time after the app is launched, but after 5 to 10 minutes the app enters in the forced closed state and no message is received.

On the other side, I have observed that there are apps that never enter that force closed state (unless sent by the user), even if they are not used for a long period of time and even across reboots (the default sms app or facebook messenger, but there are many). Other apps, including mine, do enter in the forced closed state.

My question is this: how can I code an app that is never the forced by the system into forced closed state, so that it can always receive the broadcast messages? I have tried making my app device admin, but that only stops the user form force stopping the app. After some time it doesn't receive the broadcasts anymore.


Solution

  • You probably need to manually add your app to the list of "protected apps" (or apps that are allowed to run in the background). See

    https://stackoverflow.com/a/45482394/769265

    for more information.