showIgnoreBatteryOptimizations static method

Future<DeviceSettingsRequest> showIgnoreBatteryOptimizations()

Shows the Android Ignore Battery Optimizations settings screen.

Note: In most cases, the plugin will perform normally with battery optimizations. You should only instruct the user to Ignore Battery Optimizations for your app as a last resort to resolve issues with background operation.

WARNING: Ignoring battery optimizations will cause your app to consume much more power.

showIgnoreBatteryOptimizations does not immediately redirect to the desired Device settings screen. Instead, it first returns a DeviceSettingsRequest, containing meta-data about the device (manufacturer, model, version), in addition to a flags seen and lastSeenAt, letting you know if and when you've already shown this screen to the user.

In your success-callback, it's completely up to you to instruct the user what exactly to do on that screen.

Based upon the manufacturer/model/os, a Device may not have this particular Settings screen implemented. In this case, catchError will fire.

Example

// Is Android device ignoring battery optimizations?
bool isIgnoring = await DeviceSettings.isIgnoringBatteryOptimizations;
if (!isIgnoring) {
  DeviceSettings.showIgnoreBatteryOptimizations().then((DeviceSettingsRequest request) {
    print("- Screen seen? ${request.seen} ${request.lastSeenAt}");
    print("- Device: ${request.manufacturer} ${request.model} ${request.version}");

    // If we've already shown this screen to the user, we don't want to annoy them.
    if (request.seen) {
      return;
    }

    // It's your responsibility to instruct the user what exactly
    // to do here, perhaps with a Confirm Dialog:
    showMyConfirmDialog(
      title: "Settings request",
      text: "Please disable battery optimizations for your device"
    ).then((bool confirmed) {
      if (confirmed) {
        // User clicked [Confirm] button.  Execute the redirect to settings screen:
        DeviceSettings.show(request);
      }
    });
  }).catchError((dynamic error) {
    // Depending on Manufacturer/Model/OS Version, a Device may not implement
    // a particular Settings screen.
    print(error);
  });
}

Implementation

static Future<DeviceSettingsRequest> showIgnoreBatteryOptimizations() async {
  List<dynamic> args = [IGNORE_BATTERY_OPTIMIZATIONS];

  Map request =
      (await _methodChannel.invokeMapMethod('requestSettings', args))!;
  return new DeviceSettingsRequest(
      action: request['action'],
      manufacturer: request['manufacturer'],
      model: request['model'],
      version: request['version'],
      seen: request['seen'],
      lastSeenAt: request['lastSeenAt']);
}