2

I use the following code to request ignore battery optimzation for an app. I can successfully request the permission. I am able to detect if the app is on the system's whitelist using isIgnoringBatteryOptimizations().

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>


Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

if (pm.isIgnoringBatteryOptimizations(packageName))
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else
{
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

I can see a dialog, somethings like "let the app always run in background? ", and I click yes. I assumed that it will add my app to a system battery optimization whitelist.

Here is my problem. The documentation mentioned that user can manually config the list in Settings > Battery > Battery Optimization. So I expected that once I request and granted the permission, I can see it on System Settings, and I should be able to remove it manally. But I can see nothing. I don't see there is any relationship between the request and the list from System Settings.

Do they share the same list? are they equivalent if they are not the same?

Air
  • 41
  • 2
  • 4

1 Answers1

2

So I expected that once I request and granted the permission, I can see it on System Settings

That's not what happens, actually. Documentation (https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases) says:

An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog.

It's just a permission to launch a dialog. The app isn't automatically whitelisted. Any way the user has to explicitly add it to the list.

Do they share the same list? are they equivalent if they are not the same?

There is only one list.

nandsito
  • 3,782
  • 2
  • 19
  • 26
  • Actually I could see the dialog "let the app always run in background? ", and I tap "Yes". I requested and I GRANTED the permission. After this action, I can validated that it is in the list via isIgnoringBatteryOptimizations(). I am still unable to check the list in Settings > Battery > Battery Optimization – Air Jan 07 '19 at 22:26