See how clinics ship software 3× faster with Epica
Home / Blog / Engineering
Engineering

Android Foreground Services and Battery Optimization for Continuous Patient-Monitoring Apps

Continuous vitals-monitoring apps keep getting killed in the background on Xiaomi, Oppo and Samsung devices, not because the code is wrong but because OEM skins layer extra process killers on top of stock Android. A typed foreground service, a WorkManager watchdog, sensor batching and a selective battery-optimization exemption are what actually keep monitoring alive without draining the battery.

Android phone on a clinical desk displaying a heart-rate monitoring app

Why Does Android Kill Your Monitoring App in the Background?

Stock Android already throttles background work the longer an app sits unused: Doze mode defers network access, jobs and alarms while the device is idle and stationary, and App Standby Buckets apply the same throttling to apps the system decides are low-priority based on recent usage. For a vitals-monitoring companion app, that alone is enough to turn a continuous BLE stream into a choppy one.

What actually breaks most clinic deployments, though, isn't stock Android; it's the OEM skin sitting on top of it. Xiaomi's MIUI, Oppo and OnePlus's ColorOS, and Samsung's One UI each ship their own, non-AOSP background-app killers that are far more aggressive than Doze alone, and none of them are documented the way Google's APIs are. A process that survives fine on a Pixel gets silently terminated minutes after screen-off on a budget Xiaomi phone, which is exactly the device a patient on a fixed income is likely to be using. This is why the Android engineering team we work with, YuSMP Group's android healthcare app development practice, treats OEM background behavior as a first-class design constraint rather than a bug to patch around after a field complaint.

Relying on START_STICKY alone (the flag that tells Android to try to recreate a killed service) is a bet that loses on exactly the hardware your patient population is most likely to own. The fix has to assume the OS will kill the process and build recovery around that assumption, not pretend it won't happen.

What a Foreground Service Actually Buys You (and What It Doesn't)

A foreground service is the mechanism for telling Android "this work is user-visible and shouldn't be killed casually." Starting one means calling startForegroundService() and then, within a few seconds, startForeground() with a valid, persistent notification; skip that step and the system kills the service and throws an exception. Android 14 (API level 34) tightened this further: every foreground service now has to declare a specific type in the manifest, such as health or connectedDevice for a monitoring app, and launching an untyped or undeclared foreground service is restricted outright.

That gets you real protection against the generic Android lifecycle: the app won't be swept away just because the user switched to another app. It gets you nothing against an OEM killer that decides to terminate the process anyway because it thinks the app is "draining battery," independent of whether a foreground service is running. A persistent notification and a declared type are necessary. They are not sufficient on their own.

The Persistent Notification Is Not Optional UX

Some teams try to minimize the notification to the point of being invisible, treating it as clutter rather than a requirement. That's a mistake twice over: it violates the platform contract (the notification is how the user knows monitoring is active, and Android enforces it), and on OEM skins it's often the only signal that stops the launcher's "battery saver" from flagging the app as background-idle and killing it preemptively.

Building a Watchdog Layer That Survives OEM Process Death

Because the foreground service itself can still be killed, the monitoring architecture needs a second layer whose only job is noticing when that happens and restarting it. Google's own guidance is to use WorkManager, not raw AlarmManager, for this kind of deferrable, guaranteed background work, because WorkManager adapts its scheduling to whatever power-saving constraints the OS currently has in effect rather than firing blind.

A practical watchdog pattern looks like this: a periodic WorkManager job, running every 15 minutes (the platform's practical floor for periodic work), checks whether the foreground service is alive and restarts it if not; a BOOT_COMPLETED broadcast receiver restarts monitoring after a device reboot without requiring the patient to reopen the app; and the service itself writes a heartbeat timestamp the watchdog can check, so a hung-but-technically-alive process gets caught too, not just a fully killed one.

None of this is exotic engineering. It's a small number of well-understood Android components wired together with the explicit assumption that any one of them can be killed at any time, which on a Xiaomi device with aggressive power management, it will be.

How Do You Cut Battery Drain Without Losing Monitoring Reliability?

The failure mode on the other side is just as real: an app that never gets killed because it's polling sensors continuously at full frequency, burning enough battery that the patient disables it or the OEM killer targets it more aggressively because it now looks like a genuine offender. Sensor batching, requesting hardware-batched, less-frequent samples instead of a continuous stream, is a documented technique for cutting wake-ups and power draw for exactly this kind of continuous-sensing use case; one activity-tracking app that switched from continuous to batched accelerometer sampling reported roughly a 45% reduction in battery impact from that change alone.

For a vitals-monitoring app, that usually means separating "detect an anomaly" from "stream every reading." Heart-rate or SpO2 sampling can run on a batched, lower-frequency cadence most of the time, with the app only stepping up to continuous sampling when a reading crosses a threshold that warrants closer attention. That keeps the background footprint small during normal operation and reserves the battery cost for the moments it's clinically justified.

Should You Request the Battery-Optimization Exemption?

Android lets an app ask the user to exempt it from battery optimization via REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, which stops Doze and App Standby from throttling it. Google's own battery best-practice guidance is explicit that this should be reserved for apps with a genuine continuous-operation need: requesting it reflexively burns user trust, invites uninstalls, and on some OEM app stores can draw extra scrutiny during review.

A continuous vitals or arrhythmia monitoring app clears that bar. A routine daily-sync or step-count app does not, and asking for the exemption there is an anti-pattern dressed up as reliability engineering. The distinction that should drive the decision isn't "would uninterrupted background access be convenient," it's whether a missed reading is a patient-safety gap or a UX inconvenience. For continuous monitoring tied to clinical alerting, a dropped data stream isn't a minor annoyance; it's the exact failure a care team is relying on the app to prevent, which is the honest justification for asking a patient to grant the exemption during onboarding rather than burying the prompt later.

Frequently Asked Questions

Does Android 14 require a foreground service type for health-monitoring apps?

Yes. Since API level 34, every foreground service must declare a type in the manifest (health or connectedDevice fits most vitals-monitoring use cases), and an undeclared foreground service launch is restricted.

Will disabling battery optimization get a healthtech app flagged in Play Store review?

Requesting the exemption reflexively, without a genuine continuous-operation need, is the pattern that draws scrutiny. A clearly justified clinical-monitoring use case with an honest onboarding explanation is the defensible version of the same request.

Why does the same code behave differently on Samsung vs. stock Android?

Samsung's One UI, like Xiaomi's MIUI and Oppo's ColorOS, layers its own background-app killer on top of AOSP's Doze and App Standby behavior. A service that survives fine on stock Android can still be terminated by the OEM's own power manager.

What's the difference between WorkManager and AlarmManager for a watchdog?

WorkManager is Google's recommended API for deferrable, guaranteed background work because it automatically adapts to whatever power-saving constraints the OS has active; raw AlarmManager schedules a fixed wake-up without that adaptation, which makes it less reliable under aggressive OEM power management.

How much battery does continuous BLE or sensor polling actually cost?

It varies by hardware and sampling frequency, but switching from continuous to batched sampling has been reported to cut battery impact by roughly 45% for comparable continuous-sensing workloads; batching is the lever, not the sensor itself.

Can a foreground service survive a device reboot without user interaction?

Not by itself — a reboot clears all running processes. A BOOT_COMPLETED broadcast receiver that restarts the monitoring service is the standard way to resume monitoring automatically after the device restarts.

Need this done right? Epica builds medical software end to end. Talk to us →