Check for application battery optimization status and open settings or disable it
This plugin is inspired from optimize_battery but with modern Android Gradle Plugin support.
final isIgnoring = await BatteryOptimizer.isIgnoringBatteryOptimizations();await BatteryOptimizer.openBatteryOptimizationSettings();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
Platform messages are generated with Pigeon from pigeons/messages.dart. After changing it, regenerate with:
dart run pigeon --input pigeons/messages.dart