An Android Library to detect battery optimizations settings and navigate the user to the relevant screens so that they can whitelist the app. This will prevent the app from getting killed in the background due to Battery Optimization Managers. The library supports multiple OEMs.
Add this to your module's build.gradle file:
dependencies {
// ... other dependencies
implementation 'com.waseemsabir:betterypermissionhelper:1.0.3'
}
<dependency>
<groupId>com.waseemsabir</groupId>
<artifactId>betterypermissionhelper</artifactId>
<version>1.0.3</version>
</dependency>
Other instructions for installation can be viewed here.
To keep the library small and extendable, UI elements (dialog/popups) implementation is left up to the library user. The library will not show any screens.
private val batteryPermissionHelper = BatteryPermissionHelper.getInstance()
// check whether or not Battery Permission is Available for Device
val isBatteryPermissionAvailable = batteryPermissionHelper.isBatterySaverPermissionAvailable(context = context, onlyIfSupported = true)
// Show a dialog based on availability (Implementation left to Dev) and OnClick open permission manager
buttonInDialog.setOnClickListener {
batteryPermissionHelper.getPermission(this, open = true, newTask = true)
}
private final BatteryPermissionHelper batteryPermissionHelper = BatteryPermissionHelper.Companion.getInstance();
// Check whether or not Battery Permission is Available for Device
boolean isBatteryPermissionAvailable = batteryPermissionHelper.isBatterySaverPermissionAvailable(context, /* onlyIfSupported */ true);
// Show a dialog based on availability (Implementation left to Dev) and OnClick open permission manager
buttonInDialog.setOnClickListener((view) -> {
batteryPermissionHelper.getPermission(context, /* open */ true, /* newTask */ true);
});
Currently, the library supports following devices.
- Xiaomi
- Redmi
- Poco
- Letv [ Untested ]
- HTC [ Untested ]
- Huawei
- Oppo
- Samsung
- ZTE [ Untested ]
- Meizu [ Untested ]
I am open to any PR's and contributions for any devices. I'll also try to add more devices, whenever possible.