Flutter IgnoreBatteryOptimization
Flutter IgnoreBatteryOptimization is a plugin that allows you to manage Android ignore-battery-optimization permissions in your Flutter app.
Installation
-
Add the latest version of the package to your
pubspec.yaml
(and rundart pub get
):dependencies: flutter_ignorebatteryoptimization: ^0.0.1
-
Import the package and use it in your Flutter app:
import 'package:flutter_ignorebatteryoptimization/flutter_ignorebatteryoptimization.dart';
Example
// ignore_for_file: unused_local_variable
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_ignorebatteryoptimization/flutter_ignorebatteryoptimization.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> {
final _flutterIgnorebatteryoptimizationPlugin =
FlutterIgnorebatteryoptimization();
@override
void initState() {
super.initState();
}
Future<void> openIgnorebatteryoptimizationPlugin() async {
String ignoreBatteryOptimization;
try {
ignoreBatteryOptimization = await _flutterIgnorebatteryoptimizationPlugin
.showIgnoreBatteryOptimizationSettings() ??
'Unknown ignoreBatteryOptimization';
} on PlatformException {
ignoreBatteryOptimization = 'Failed to show ignoreBatteryOptimization.';
}
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter IgnoreBatteryOptimization'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
openIgnorebatteryoptimizationPlugin();
},
child: const Text(
"Request ignore-battery-optimization permission"),
),
],
),
),
),
);
}
}