battery_optimization_permission
Android battery optimization (Doze) whitelist helper + best‑effort OEM Auto‑start / Background settings shortcuts.
nh97.co.in
✨ Features
- ✅ Check if the app is whitelisted (ignoring battery optimizations) on Android 6.0+
- ⚙️ Prompt the user to allow “Ignore battery optimizations”
- ⚙️ Open system battery optimization settings
- ⚙️ Open app settings
- 🧩 Best‑effort OEM Auto‑start / Background settings deep links:
- Xiaomi / Redmi / Poco (MIUI / HyperOS)
- OPPO / Realme / OnePlus (ColorOS family)
- Vivo / iQOO
- Samsung
- ✅ One call flow (best UX): prompt → OEM screen → system settings → app settings
Installing
Add to pubspec.yaml:
dependencies:
battery_optimization_permission: ^1.1.0
Or use the CLI:
flutter pub add battery_optimization_permission
Then run flutter pub get if needed.
Android manifest permission
This plugin declares:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
If you prefer to declare it yourself, keep the same permission in your app manifest.
Usage
import 'dart:io';
import 'package:battery_optimization_permission/battery_optimization_permission.dart';
Future<void> example() async {
if (!Platform.isAndroid) return;
// Check
final whitelisted =
await BatteryOptimizationPermission.isIgnoringBatteryOptimizations();
if (!whitelisted) {
// Best UX: prompt → OEM screen → settings fallbacks
final ok = await BatteryOptimizationPermission.ensureBatteryWhitelist(
tryOemScreens: true,
openSettingsFallbacks: true,
);
// ok == true only if the app is whitelisted right now.
// If ok == false, settings screens were opened for the user to complete manually.
}
// Open system battery optimization screen
await BatteryOptimizationPermission.openBatteryOptimizationSettings();
// Best‑effort OEM background/auto‑start settings (may return false)
final opened = await BatteryOptimizationPermission.openOemAutoStartSettings();
if (!opened) {
// Consider showing your in‑app guidance / help page
}
}
Result‑based behavior
requestIgnoreBatteryOptimizations()returns the current state after the user returns from the system dialog.ensureBatteryWhitelist()returnstrueonly if your app is already whitelisted or becomes whitelisted via the prompt.
Notes on Android versions
- Android < 6.0 (API < 23): Doze/battery optimizations don’t apply. The check safely reports whitelisted.
- OEM Auto‑start settings: Paths differ by device/ROM and may not exist. OEM shortcuts are best‑effort, and the plugin falls back to system settings or app settings.
Tooling
Recommended (matches this plugin template):
- Kotlin Gradle Plugin: 1.9.x
- Android Gradle Plugin: 8.3+
- compileSdk: 34
- Flutter: 3.x
Tips
- Explain clearly why the whitelist is required (reminders, attendance/shift alerts, background services) before showing the system prompt.
- Handle
falseresults fromopenOemAutoStartSettings()gracefully. - If
ensureBatteryWhitelist()returnsfalse, show a short step list in your UI (OEM + system screens).
Example
See example/ for a runnable app.
Changelog
See CHANGELOG.md for release notes.
License
MIT