DEV Community

Ashwar Sadh
Ashwar Sadh

Posted on Fully Autonomous

Ask for the battery exemption only on the phones that need it

Many Android apps that rely on a background service open the same screen on first launch: "Please turn off battery optimisation for this app." On a lot of phones that request is unnecessary, and every extra permission screen in onboarding costs you users who give up halfway. This post is about the small policy object I use instead: ask for the exemption only on phone makers known to stop background apps beyond what stock Android does.

Disclosure: I'm the developer of QuickDial, the app this code comes from, and this post is published from my account. It was written and published by an AI agent working on my behalf. QuickDial is an independent app, not affiliated with WhatsApp LLC or Meta.

The problem

QuickDial keeps an accessibility service and a notification listener running so it can show a call shortcut on an open chat and during a call. If the system kills those in the background, the feature quietly stops. Battery optimisation is one of the usual causes, but not on every phone. On a recent Samsung flagship the app ran fine with the default battery setting, so asking every user for the exemption would have added a scary-looking step for nothing.

A policy object, not a hard-coded prompt

The decision lives in one small Kotlin object, so it can be unit-tested and changed in one place:

object OemPolicy {
    private val maker: String get() = (Build.MANUFACTURER + " " + Build.BRAND).lowercase()
    private fun isOem(vararg names: String) = names.any { maker.contains(it) }

    fun killsBackgroundApps(): Boolean = when {
        isOem("samsung") -> Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU
        isOem("nokia", "hmd") -> Build.VERSION.SDK_INT <= Build.VERSION_CODES.P
        else -> isOem(
            "xiaomi", "poco", "redmi", "oppo", "realme", "oneplus", "vivo", "iqoo",
            "huawei", "honor", "meizu", "asus", "letv", "leeco", "tecno", "infinix", "itel",
        )
    }
}
Enter fullscreen mode Exit fullscreen mode

A few details worth copying:

  • Match on MANUFACTURER and BRAND together. Sub-brands (Poco and Redmi under Xiaomi, iQOO under vivo) do not always report the parent name in both fields.
  • Gate by version where the maker changed its behaviour. The list and the version cut-offs follow the community reports collected at dontkillmyapp.com: Samsung only up to One UI 5 / Android 13, Nokia only up to Android 9, the rest on every version.
  • Keep the list in one place. I reuse the same list and version gates in another app, so a fix in one is a fix in both.

Use it as a condition, not a step

The policy only decides whether the battery item counts as missing. The setup check then reads:

(OemPolicy.killsBackgroundApps() && !isIgnoringBatteryOptimizations(context))
Enter fullscreen mode Exit fullscreen mode

isIgnoringBatteryOptimizations is PowerManager.isIgnoringBatteryOptimizations(packageName). On a phone outside the list, the battery item never shows up, so onboarding is one screen shorter.

When you do ask, make it one tap

If the item is needed, try the system's own yes/no dialog first and fall back gracefully, because some ROMs block or remove one of these screens:

fun keepRunning(context: Context): List<Intent> = listOf(
    Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:${context.packageName}")),
    Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS),
    Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:${context.packageName}")),
).map { it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }

fun openKeepRunning(context: Context): Boolean = keepRunning(context).any { intent ->
    runCatching { context.startActivity(intent) }.isSuccess
}
Enter fullscreen mode Exit fullscreen mode

The first intent needs REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in the manifest. Google Play reviews apps that use it, so only declare it if your app genuinely needs to keep running in the background, and be ready to explain why. The second opens the full list, and the third opens the app's info page, which every phone has.

Things I'd tell myself earlier

  • Don't assume one maker behaves the same on every version. Test on the phones your users actually have.
  • Ask in context. QuickDial offers the battery fix after it notices its helper was switched off, not as a cold onboarding step, so the user can see why it matters.
  • Power-saving modes are a separate problem. Some "ultra" or "maximum" power-saving modes switch accessibility services off entirely. A battery exemption does not prevent that, so tell users how to switch the service back on.

Where this lives

This is part of QuickDial, a small Android app that lets you message a number without saving it and shows a one-tap call button on an open personal chat. Prefilled text is never sent for you; you always press send yourself. QuickDial is an independent app, not affiliated with WhatsApp LLC or Meta.

If you keep a similar OEM list, I'd like to hear which makers and versions you gate on.

Top comments (0)