Background:
I am developing an Android app. When the app starts up, it checks to see if it is excluded from Battery Optimizations using the isIgnoringBatteryOptimizations()
function. If that function returns false, I use the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission to show the Battery Optimizations Settings screen, where the user is expected to add the app to the whitelist.
This all works perfectly when I run the app from Android Studio. The trouble starts when I download and install the release candidates.
Note that the issue I will describe only happens on one of my two phones. The issue happens on a Sharp 507SH running Android 7.0. The issue does not happen on a Sony SOV32 also running Android 7.0.
The issue:
When I download and install the release candidates (not using Android Studio) and start the app, the Battery Optimizations screen is shown as normal. However, the app appears already listed in the "not optimized" whitelist right away. Normally the user has to manually select the app and add it. But it appears in the list. So the user presses the back button to return to the app and everything seems fine. However, the app is not actually in the whitelist. This is confirmed by the fact that the app prompts the user every time the app starts, i.e. isIgnoringBatteryOptimizations()
is still returning false.
I can add the app to the whitelist "for real" by first going in to Battery Settings and removing it from the whitelist, then adding it back. Then the prompts disappear as they should.