Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Use the ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS for newer Android devices #151

Closed
alexstyl opened this issue Sep 2, 2017 · 2 comments

Comments

@alexstyl
Copy link
Owner

alexstyl commented Sep 2, 2017

Some people mention that sometimes the application does not show an notification to them. This is probably do to this:

ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS

added in API level 23
String ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
Activity Action: Show screen for controlling which apps can ignore battery optimizations.

Input: Nothing.

Output: Nothing.

You can use PowerManager.isIgnoringBatteryOptimizations() to determine if an application is already ignoring optimizations. You can use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to ask the user to put you on this list.

Constant Value: "android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS"
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

added in API level 23
String ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
Activity Action: Ask the user to allow an app to ignore battery optimizations (that is, put them on the whitelist of apps shown by ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS). For an app to use this, it also must hold the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission.

Note: most applications should not use this; there are many facilities provided by the platform for applications to operate correctly in the various power saving modes. This is only for unusual applications that need to deeply control their own execution, at the potential expense of the user's battery life. Note that these applications greatly run the risk of showing to the user as high power consumers on their device.

Input: The Intent's data URI must specify the application package name to be shown, with the "package" scheme. That is "package:com.my.app".

Output: Nothing.

You can use PowerManager.isIgnoringBatteryOptimizations() to determine if an application is already ignoring optimizations.

Constant Value: "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
https://developer.android.com/reference/android/provider/Settings.html#ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

@alexstyl
Copy link
Owner Author

alexstyl commented Sep 2, 2017

Helpful snippet:

    public abstract void disableOptimizations(Context context);

    public static BatteryOptimizationDisabler create() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return new NoOpBatteryOptimizationDisabler();
        } else {
            return new AndroidBatteryOptimizationDisabler();
        }
    }

    private static class NoOpBatteryOptimizationDisabler extends BatteryOptimizationDisabler {

        @Override
        public void disableOptimizations(Context context) {
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private static class AndroidBatteryOptimizationDisabler extends BatteryOptimizationDisabler {

        @SuppressLint("BatteryLife") // we need gifts, it's acceptable, shush.
        @Override
        public void disableOptimizations(Context context) {
            PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
            String packageName = context.getApplicationContext().getPackageName();
            boolean ignoringOptimizations = powerManager.isIgnoringBatteryOptimizations(packageName);

            if (ignoringOptimizations) {
                return;
            }

            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            context.startActivity(intent);
        }
    }
}```

@alexstyl
Copy link
Owner Author

alexstyl commented Sep 5, 2018

Doesn't seem to be needed anymore after #207

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant