How to remove Battery Optimization restrictions for Device Owner app

589 Views Asked by At

Battery optimization settings creates some difficulties for the device owner app I am currently developing, such as not receiving BOOT_COMPLETED broadcast in MIUI. I want to know if there is any way of removing restrictions of device owner app programmatically, like granting permissions to the device owner app itself. I could not find about this anywhere or alternative solution to the similar problems, such as keeping background services alive and auto starting services on boot.

2

There are 2 best solutions below

0
digrec On BEST ANSWER

I didn't find a way to whitelist an app from doze mode silently and programatically. The only option is to ask user of the app to do it for you.

The intent action that takes the user to Battery Optimization settings to do this is described here: https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases

I have seen other MDMs using this approach too.

0
Wolfie On

You can use this library that helps navigate your user to the settings screen that whitelists your app for these EOMs: https://github.com/WaseemSabir/BatteryPermissionHelper

Can be used like this:

dependencies {
    // ... other dependencies
    implementation 'com.waseemsabir:betterypermissionhelper:1.0.0'    
}
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
dialog.setOnClickListener {
    batteryPermissionHelper.getPermission(this, open = true, newTask = true)
}