requestIgnoreBatteryOptimizations method
Requests for app to be excluded from battery optimizations to aid running a background process
Only available on Android devices, due to limitations with other operating systems.
Background downloading is complicated: see the documentation website for more information.
If requestIfDenied
is true
(default), and the permission has not been granted, an intrusive system dialog/screen will be displayed. If false
, this method will only check whether it has been granted or not.
Will return true
if permission was granted, false
if the permission was denied.
Implementation
Future<bool> requestIgnoreBatteryOptimizations({
bool requestIfDenied = true,
}) async {
if (Platform.isAndroid) {
final PermissionStatus status =
await Permission.ignoreBatteryOptimizations.status;
if ((status.isDenied || status.isLimited) && requestIfDenied) {
final PermissionStatus statusAfter =
await Permission.ignoreBatteryOptimizations.request();
if (statusAfter.isGranted) return true;
return false;
} else if (status.isGranted) {
return true;
} else {
return false;
}
} else {
throw PlatformException(
code: 'notAndroid',
message:
'The background download feature is only available on Android due to internal limitations.',
);
}
}