5

I don't mean nothing happens. The dialog pops up:

"Let app always run in background? Allow / Deny

But then if I go into settings, App, Battery Optimizations, my app is still set to "Optimizing Battery use". I can then manually switch it to "Don't Optimize", but according to the docs and all other questions on here, using the ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent, and clicking allow, should change this setting.

I am using the:

Also, this app is for research, and will not go on the store, so I am not worried about Play Store policies.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent();
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
        }
    }

I am using the above code. It fires, brings up a dialog to "Let app always run in background?" I press Allow.

My question is what pressing "Allow" actually does, because if you then go into the App settings, and look at Battery Optimization, Settings says the Application is still "Optimizing Battery Usage".

So it would appear the above intent, and pressing Allow, does not actually do anything what so ever.

Geordie Wicks
  • 1,065
  • 1
  • 11
  • 27
  • It is unclear what your question is. You might also want to post a [mcve] so we can see how you are setting up the `Intent` (notably, the `Uri` that needs to be in it for this particular `Intent` action to work). – CommonsWare Oct 02 '18 at 23:39
  • On further examination, it appears it changes to "Not Optimized", after restarting the phone. I tried force closing settings, but that does not change anything, it appears it only changes after switching the phone off and on again. – Geordie Wicks Oct 02 '18 at 23:53
  • I made a work around posted in another [question](https://stackoverflow.com/a/47685874/3174791) – Edgar May 17 '19 at 16:48

1 Answers1

0

I put your code in a scrap project, added the android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission to the manifest, ran it on a Google Pixel (Android 9.0), and everything worked as expected. No reboot required.

So, either:

  • Your problem is only on Android 8.1 or older devices and reflects a platform bug that they fixed in 9.0, or

  • Your problem is tied to certain devices from certain manufacturers

My guess is that it is the latter, but you would need to do more extensive testing to try to determine that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am using a Pixel XL on android 9. When you say everything worked as expected, does that mean after running project, if you go to settings -> apps -> project -> advanced -> battery, it says "not optimized"? – Geordie Wicks Oct 03 '18 at 00:36
  • @GeordieWicks: Correct. – CommonsWare Oct 03 '18 at 10:43
  • Ok thanks, just one of those weird Android things I guess. My feeling is it must be working, the settings app is just not updating dynamically. – Geordie Wicks Oct 03 '18 at 17:27
  • 1
    @GeordieWicks I tried on my Pixel 2, api level 28, and I see the same behavior you see. The permission in the settings page doesn't change until I reboot. – James Moore Oct 03 '18 at 21:19
  • However, I just tried using "adb shell dumpsys deviceidle" before and after hitting the accept button. After hitting accept, my app package is immediately listed in the "Whitelist user apps:" section, and the app id is listed in "Whitelist (except idle) all app ids:", "Whitelist user app ids", and "Whitelist all app ids" section. – James Moore Oct 03 '18 at 21:30
  • @JamesMoore, just add it to the very long list of nonsensical android behavior I guess. Good to know that it does actually do something though, I didn't want to ask the user for another scary permission if it was not doing anything! – Geordie Wicks Oct 04 '18 at 00:27
  • I am also seeing the same behaviour where I need to restart the device for this to take affect. But currently, I am only seeing it in Pixel 2 running Android 10. I'm assuming this is a bug in one of the recent updates or quiet updates google is bound to be doing. – Alon Minski Jan 06 '20 at 09:40
  • in oneplus 5 with Android 10, i do press Allow button but when i go to settings it shows battery optimized .. strange but true – Rohit Oct 04 '20 at 10:55
  • I faced the same issue. what was happening when we click on ALLOW is that the app was opted out from battery optimisation. but it takes some time to reflect. close the devices settings app and reopen again to check if it has reflected the proper change. – khubaib May 11 '22 at 08:52