1

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.

Eugen
  • 55
  • 1
  • 7
  • possibly a duplicate of : https://stackoverflow.com/questions/9477606/disabling-force-stop-button-in-android – Boukharist Apr 04 '18 at 11:34
  • Ask Huawei. In conventional Android, the only time apps are in the stopped state are on first install (before the user has run the launcher activity) and after the user presses "Force Stop". If Huawei is putting apps into the stopped state in other situations, that is something that Huawei did, and you will need to ask them why. BTW, the button is "Force Stop", not "force close" -- "force close" is a separate concept, related to crashed apps. – CommonsWare Apr 04 '18 at 11:36
  • @Boukharist not related to the linked question – David Wasser Apr 05 '18 at 13:01

1 Answers1

0

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • That's what I did. It turns out that on huawei phones (maybe on other phones too) some apps are put in force stopped state by the system - perhaps based on heuristics about power consumption - because other apps don't share this fate. – Eugen Apr 06 '18 at 11:06
  • Hmmm. The "force stop" button is greyed out if the app is not running. Do you have a `Service` that runs all the time (ie: START_STICKY)? If not, your app won't be running all the time. Other apps (like FB Messenger) have a `Service` that is running all the time, so the `force stop` button is never greyed out. – David Wasser Apr 06 '18 at 11:09
  • I made a dummy app composed of: a main activity, a receiver for android.intent.action.PHONE_STATE that makes a toast and a service that does nothing except returning START_STICKY. The service is started in the main activity. After the app is installed it runs one time and then I slide it off from the recently used tasks. The force stop button is still enabled after this. I make a phone call, the broadcast is received, then I turn the screen off. After I turn it on again (in a couple of seconds), the force stop button is grayed out and the receiver doesn't receive broadcasts anymore. – Eugen Apr 06 '18 at 11:31
  • And you have added this app manually to the list of "protected apps" that are allowed to run in the background? – David Wasser Apr 06 '18 at 11:54
  • I present a dialog to the user to put the app on protected apps list: https://stackoverflow.com/questions/31638986/protected-apps-setting-on-huawei-phones-and-how-to-handle-it – Eugen Apr 06 '18 at 12:30
  • But for your testing did you add the app to the list of protected apps? – David Wasser Apr 06 '18 at 12:40
  • So if you add your app to the list of protected apps, it gets the broadcasts and works as it should? – David Wasser Apr 06 '18 at 12:51
  • The force stop button stays enabled and the broadcasts are received. – Eugen Apr 06 '18 at 13:34
  • OK then. Problem solved! You can accept my answer by clicking the green checkmark next to the answer. This will help others who may have this problem and will also get this question off the "unanswered questions" list. – David Wasser Apr 06 '18 at 14:18