Skip to content

About

Checks for android battery optimization status and allows a user to enable or disable it

Resources

Stars

0 stars

Watchers

4 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

battery_optimizer

Check for application battery optimization status and open settings or disable it

Credit

This plugin is inspired from optimize_battery but with modern Android Gradle Plugin support.

Check if app is ignoring battery optimization

final isIgnoring = await BatteryOptimizer.isIgnoringBatteryOptimizations();

Take user to battery optimization settings

await BatteryOptimizer.openBatteryOptimizationSettings();

Ask the user to stop optimizing battery usage

final result = await BatteryOptimizer.stopOptimizingBatteryUsage();
switch (result) {
  case BatteryOptimizationRequestResult.granted:
    // The app is now ignoring battery optimizations.
  case BatteryOptimizationRequestResult.denied:
    // The user just said no.
  case BatteryOptimizationRequestResult.previouslyDenied:
    // The user said no before, so the dialog was not shown.
  case BatteryOptimizationRequestResult.unsupported:
    // Not running on Android.
}

This shows the system battery optimization dialog and waits for the user's answer.

If the user declines, the plugin remembers it and won't show the dialog again. Later calls return previouslyDenied right away. To ask again, for example after the user turns on a setting in your app, call:

await BatteryOptimizer.resetDeclinedRequest();

Use BatteryOptimizer.hasUserDeclinedRequest() to check whether the user has declined.

Add the permission in Manifest file

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

It uses the intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS). Which is not recommended unless its an exemption case. Such as the applications main functionality needs the battery optimisation to be disabled.

Read more here : https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases

Development

Platform messages are generated with Pigeon from pigeons/messages.dart. After changing it, regenerate with:

dart run pigeon --input pigeons/messages.dart

About

Checks for android battery optimization status and allows a user to enable or disable it

Resources

Stars

0 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages