How to deal with Huawei's and Xiaomi's "battery optimizations"
Asked Answered
W

1

12

I have an app that listens for notifications via NotificationListenerService. My app survives both the Doze mode and app stand by on almost any phone (if properly set-up e.g. exception or a foreground service).

Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life". These can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the app. Even exceptions to their own "optimizations" does not reliably fix this. Only completely disabling all their optimizations e.g. enabling "high performance mode" might fix this. But users do not want to disable these options.

How do you deal with these cases? And how can we stop manufacturers to stop messing with core Android? Not to mention the funny ANRs & crashes in Zygote :(

Wordsworth answered 6/11, 2017 at 21:2 Comment(0)
A
3

Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life".

Yes Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.

What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.

These can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the app.

These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.

Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.

Let me know if you find one ;)

Xiaomi app auto start settings screen Xiaomi app battery saver settings screen

Atterbury answered 9/12, 2017 at 14:38 Comment(5)
From my own testing I know that they whitelists do not work reliably, especially Xiaomi. Problem is many users have no idea what a battery saver or whitelist is and they give bad ratings and never respond to replies. I remember two years ago that even Viber was not on their automatic whitelist (it is now) and it stopped working the moment the app was minimized. Terminating the app in the background is not a problem, the problem is that the app is never started again on its own, as it should be and as is on pure Android.Wordsworth
I would rather show a pop-up telling my users to buy a real phone.Wordsworth
haha i feel the same...but the problem is Xiaomi devices are being widely used in market so we can't help it.Atterbury
I've read that asking your users to whitelist your app will get it suspended from Google Play...Inellineloquent
What you have heard must be about the 'Ignore battery optimizations' permission. This permission is allowed only for certain use cases as mentioned here: developer.android.com/training/monitoring-device-state/…. In case of what is discussed in the answer, google doesn't mind what you ask user to do. The prompt for user to whitelist your app is necessary because otherwise you wont be able to send push notifications or process anything at all. No broadcasts, etc. will be sent to you when app is killed.Atterbury

© 2022 - 2024 — McMap. All rights reserved.