{"slug": "ask-for-the-battery-exemption-only-on-the-phones-that-need-it", "title": "Ask for the battery exemption only on the phones that need it", "summary": "A developer of the Android app QuickDial published a Kotlin OemPolicy object that requests battery-optimization exemptions only on device makers known to aggressively kill background apps, such as Xiaomi, Oppo, Vivo and Huawei, while skipping the prompt on newer Samsung and Nokia devices. The policy combines Build.MANUFACTURER and Build.BRAND with SDK-version checks, and pairs with a fallback chain of intents that tries the system exemption dialog, then the full battery settings list, then the app info page.", "body_md": "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.\n\n*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.*\n\nQuickDial 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.\n\nThe decision lives in one small Kotlin object, so it can be unit-tested and changed in one place:\n\n```\nobject OemPolicy {\n    private val maker: String get() = (Build.MANUFACTURER + \" \" + Build.BRAND).lowercase()\n    private fun isOem(vararg names: String) = names.any { maker.contains(it) }\n\n    fun killsBackgroundApps(): Boolean = when {\n        isOem(\"samsung\") -> Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU\n        isOem(\"nokia\", \"hmd\") -> Build.VERSION.SDK_INT <= Build.VERSION_CODES.P\n        else -> isOem(\n            \"xiaomi\", \"poco\", \"redmi\", \"oppo\", \"realme\", \"oneplus\", \"vivo\", \"iqoo\",\n            \"huawei\", \"honor\", \"meizu\", \"asus\", \"letv\", \"leeco\", \"tecno\", \"infinix\", \"itel\",\n        )\n    }\n}\n```\n\nA few details worth copying:\n\n`MANUFACTURER` and `BRAND` together.\nThe policy only decides whether the battery item counts as missing. The setup check then reads:\n\n```\n(OemPolicy.killsBackgroundApps() && !isIgnoringBatteryOptimizations(context))\n```\n\n`isIgnoringBatteryOptimizations` is `PowerManager.isIgnoringBatteryOptimizations(packageName)`. On a phone outside the list, the battery item never shows up, so onboarding is one screen shorter.\n\nIf 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:\n\n```\nfun keepRunning(context: Context): List<Intent> = listOf(\n    Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse(\"package:${context.packageName}\")),\n    Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS),\n    Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse(\"package:${context.packageName}\")),\n).map { it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }\n\nfun openKeepRunning(context: Context): Boolean = keepRunning(context).any { intent ->\n    runCatching { context.startActivity(intent) }.isSuccess\n}\n```\n\nThe 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.\n\nThis is part of [QuickDial](https://play.google.com/store/apps/details?id=com.ashwar.quickdialbridge&utm_source=devto&utm_medium=article&utm_campaign=2026-w43), 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.\n\nIf you keep a similar OEM list, I'd like to hear which makers and versions you gate on.", "url": "https://wpnews.pro/news/ask-for-the-battery-exemption-only-on-the-phones-that-need-it", "canonical_source": "https://dev.to/ashwar_sadh_f7abe6f82c8e9/ask-for-the-battery-exemption-only-on-the-phones-that-need-it-1i1o", "published_at": "2026-10-20 06:14:21+00:00", "updated_at": "2026-10-20 06:17:10.797867+00:00", "lang": "en", "topics": ["developer-tools", "ai-agents"], "entities": ["QuickDial", "Samsung", "Nokia", "HMD", "Xiaomi", "Google Play", "WhatsApp", "Meta"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/ask-for-the-battery-exemption-only-on-the-phones-that-need-it", "markdown": "https://wpnews.pro/news/ask-for-the-battery-exemption-only-on-the-phones-that-need-it.md", "text": "https://wpnews.pro/news/ask-for-the-battery-exemption-only-on-the-phones-that-need-it.txt", "jsonld": "https://wpnews.pro/news/ask-for-the-battery-exemption-only-on-the-phones-that-need-it.jsonld"}}