Flutter Plugin to Check and Disable Battery Optimizations

disable_battery_optimization 

Flutter plugin to check and disable battery optimizations. Also shows custom steps to disable the manufacturer specific optimizations on devices like mi, xiaomi, samsung, oppo, huawei, oneplus etc


Auto start 

bool isAutoStartEnabled = await DisableBatteryOptimization.isAutoStartEnabled;

This will return true, for the devices which doesn't support auto start configuration as well for the devices on which, user has clicked on positive action for the auto start configuration dialog.

await DisableBatteryOptimization.showEnableAutoStartSettings("Enable Auto Start", "Follow the steps and enable the auto start of this app");

This will show a dialog with steps to enable auto start permission on the current device (if it's available)

Note: We cannot determine if the user has actually enabled the auto start permission or not. We can only show a dialog with the steps to enable.


Android Battery Optimization 

bool isBatteryOptimizationDisabled = await DisableBatteryOptimization.isBatteryOptimizationDisabled;

This will return true, only if the user has disabled the 'native' battery optimization for this app.

await DisableBatteryOptimization.showDisableBatteryOptimizationSettings()

This will ask for the permission to disable battery optimization for the app.


Manufacturer specific Battery Optimization 

bool isManBatteryOptimizationDisabled = await DisableBatteryOptimization.isManufacturerBatteryOptimizationDisabled;

This will return true, for the devices which doesn't support manufacturer specific battery optimization configuration as well for the devices on which, user has clicked on positive action for the manufacturer specific battery optimization configuration dialog.

await DisableBatteryOptimization.showDisableManufacturerBatteryOptimizationSettings("Your device has additional battery optimization", "Follow the steps and disable the optimizations to allow smooth functioning of this app");

This will show a dialog with steps to disable the manufacturer specific battery optimization on the current device (if it's available)

Note: We cannot determine if the user has actually disabled the manufacturer specific battery optimization or not. We can only show a dialog with the steps to disable it.


There are other helper methods available to check if all the permissions are enabled or disabled.

await DisableBatteryOptimization.isAllBatteryOptimizationDisabled
await DisableBatteryOptimization.showDisableAllOptimizationsSettings

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add disable_battery_optimization

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  disable_battery_optimization: ^1.1.1

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:disable_battery_optimization/disable_battery_optimization.dart';

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:disable_battery_optimization/disable_battery_optimization.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Disable Battery Optimizations Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              MaterialButton(
                  child: Text("Is Auto Start Enabled"),
                  onPressed: () async {
                    bool isAutoStartEnabled =
                        await DisableBatteryOptimization.isAutoStartEnabled;
                    print(
                        "Auto start is ${isAutoStartEnabled ? "Enabled" : "Disabled"}");
                  }),
              MaterialButton(
                  child: Text("Is Battery optimization disabled"),
                  onPressed: () async {
                    bool isBatteryOptimizationDisabled =
                        await DisableBatteryOptimization
                            .isBatteryOptimizationDisabled;
                    print(
                        "Battery optimization is ${!isBatteryOptimizationDisabled ? "Enabled" : "Disabled"}");
                  }),
              MaterialButton(
                  child: Text("Is Manufacturer Battery optimization disabled"),
                  onPressed: () async {
                    bool isManBatteryOptimizationDisabled =
                        await DisableBatteryOptimization
                            .isManufacturerBatteryOptimizationDisabled;
                    print(
                        "Manufacturer Battery optimization is ${!isManBatteryOptimizationDisabled ? "Enabled" : "Disabled"}");
                  }),
              MaterialButton(
                  child: Text("Are All Battery optimizations disabled"),
                  onPressed: () async {
                    bool isAllBatteryOptimizationDisabled =
                        await DisableBatteryOptimization
                            .isAllBatteryOptimizationDisabled;
                    print(
                        "All Battery optimizations are disabled ${isAllBatteryOptimizationDisabled ? "True" : "False"}");
                  }),
              MaterialButton(
                  child: Text("Enable Auto Start"),
                  onPressed: () {
                    DisableBatteryOptimization.showEnableAutoStartSettings(
                        "Enable Auto Start",
                        "Follow the steps and enable the auto start of this app");
                  }),
              MaterialButton(
                  child: Text("Disable Battery Optimizations"),
                  onPressed: () {
                    DisableBatteryOptimization
                        .showDisableBatteryOptimizationSettings();
                  }),
              MaterialButton(
                  child: Text("Disable Manufacturer Battery Optimizations"),
                  onPressed: () {
                    DisableBatteryOptimization
                        .showDisableManufacturerBatteryOptimizationSettings(
                            "Your device has additional battery optimization",
                            "Follow the steps and disable the optimizations to allow smooth functioning of this app");
                  }),
              MaterialButton(
                  child: Text("Disable all Optimizations"),
                  onPressed: () {
                    DisableBatteryOptimization.showDisableAllOptimizationsSettings(
                        "Enable Auto Start",
                        "Follow the steps and enable the auto start of this app",
                        "Your device has additional battery optimization",
                        "Follow the steps and disable the optimizations to allow smooth functioning of this app");
                  })
            ],
          ),
        ),
      ),
    );
  }
}

Download details:

Author: jvapps.in

Source: https://github.com/pvsvamsi/Disable-Battery-Optimizations

#flutter #android #ios

Flutter Plugin to Check and Disable Battery Optimizations
1.05 GEEK