optimize_battery 0.0.4 copy "optimize_battery: ^0.0.4" to clipboard
optimize_battery: ^0.0.4 copied to clipboard

PlatformAndroid

Check for battery optimization status and open settings or disable it

example/lib/main.dart

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String isBatterIgnoredText = 'Unknown';

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Optimize Battery Plugin Example'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // Check if app is ignoring battery optimizations
                ElevatedButton(
                    child:
                        const Text("Check if battery optimization is enabled"),
                    onPressed: () async {
                      //Check if battery optimization is enabled
                      final isIgnored = await OptimizeBattery
                          .isIgnoringBatteryOptimizations();
                      setState(() {
                        isBatterIgnoredText =
                            isIgnored ? "Ignored" : "Not Ignored";
                      });
                    }),
                Text("Battery Optimization is $isBatterIgnoredText"),
                const SizedBox(height: 20),

                // Open battery optimization settings
                ElevatedButton(
                  child: const Text("Open Battery Optimisation Settings"),
                  onPressed: () =>
                      // Open battery optimization settings
                      OptimizeBattery.openBatteryOptimizationSettings(),
                ),
                const SizedBox(height: 20),

                //Disable battery optimization
                ElevatedButton(
                    child: const Text("Disable Battery Optimisation"),
                    onPressed: () async {
                      //Stops the battery optimization
                      await OptimizeBattery.stopOptimizingBatteryUsage();
                      setState(() {
                        isBatterIgnoredText = "Unknown";
                      });
                    }),
              ],
            ),
          )),
    );
  }
}
32
likes
160
points
1.53k
downloads

Publisher

unverified uploader

Weekly Downloads

Check for battery optimization status and open settings or disable it

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on optimize_battery