Skip to content

This Library helps whitelist your Android App in Battery Settings, to prevent the app from getting killed due to Battery Optimisations.

License

Notifications You must be signed in to change notification settings

WaseemSabir/BatteryPermissionHelper

Repository files navigation

BatteryPermissionHelper

License: MIT Android Build Unit Tests API

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.

Installation

Gradle

Add this to your module's build.gradle file:

dependencies {
    // ... other dependencies
    implementation 'com.waseemsabir:betterypermissionhelper:1.0.3'    
}

Maven

<dependency>
    <groupId>com.waseemsabir</groupId>
    <artifactId>betterypermissionhelper</artifactId>
    <version>1.0.3</version>
</dependency>

Other instructions for installation can be viewed here.

Usuage

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)
}

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.

Currently, the library supports following devices.

  1. Xiaomi
  2. Redmi
  3. Poco
  4. Letv [ Untested ]
  5. HTC [ Untested ]
  6. Huawei
  7. Oppo
  8. Samsung
  9. ZTE [ Untested ]
  10. Meizu [ Untested ]

I am open to any PR's and contributions for any devices. I'll also try to add more devices, whenever possible.

Relevant Work