MyFamilyTracker
Real-time family location sharing — Firebase Realtime DB for sub-second propagation, WorkManager + ForegroundService for OS-compliant background collection, geofencing via Google Maps API.
Three tools for background work, three different jobs. Picking the wrong one means battery drain, broken behavior on OEM devices, or work that silently stops running.
On this page
Background work is one of the most misunderstood areas of Android development. Developers reach for Services when they should use WorkManager, use WorkManager for things that should be Coroutines, and forget that OEM battery optimizations will kill both.
Here's the actual breakdown.
Immediate, short-lived work — triggered by user action, needs to complete before the user navigates away. Use Coroutines.
Deferrable, guaranteed work — needs to survive process death, app kills, device restarts. Use WorkManager.
Long-running foreground work — active while the user can see a persistent notification. Use ForegroundService.
Each category has one right answer. The confusion comes from using the wrong tool for the category.
If the user tapped a button and you need to make an API call, save data, or process something — Coroutines in the ViewModel scope.
fun onSaveButtonClicked() {
viewModelScope.launch {
val result = repository.saveData(currentData)
_uiState.update { it.copy(saved = result.isSuccess) }
}
}viewModelScopeDon't use WorkManager for this. WorkManager is for work that must complete even if the user kills the app. Using it for a button tap is over-engineering that adds latency and complexity.
Syncing data, uploading photos, processing reports — work that must complete eventually, even after a restart.
class SyncWorker(
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
return try {
repository.syncData()
Result.success()
} catch (e: Exception) {
if (runAttemptCount < 3) Result.retry() else Result.failure()
}
}
}
// Schedule it
val syncRequest = PeriodicWorkRequestBuilder<SyncWorker>(1, TimeUnit.HOURS)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"data_sync",
ExistingPeriodicWorkPolicy.KEEP,
syncRequest
)WorkManager persists work to a database and reschedules across restarts. It also respects battery optimization and Doze mode — it defers work when the device is idle rather than fighting the OS.
Location tracking, music playback, file downloads. Work that the user knows is active (visible notification), that runs continuously.
class LocationTrackingService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = buildNotification()
startForeground(NOTIFICATION_ID, notification)
serviceScope.launch {
locationManager.locationUpdates()
.collect { location -> repository.saveLocation(location) }
}
return START_STICKY
}
}ForegroundService requires a visible notification — that's the contract with the user. It stays alive while the notification is visible. The OS will not kill it without user action.
Stock Android behavior and Samsung/Xiaomi/Huawei behavior are different. Aggressive battery optimization on OEM devices kills background work — including WorkManager — far more aggressively than AOSP.
For critical background work: prompt users to whitelist your app from battery optimization.
PowerManager.isIgnoringBatteryOptimizations()Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)It's an awkward UX. It's also the only reliable solution.
The most common mistake: reaching for WorkManager because "it's more reliable" for work that's actually immediate and doesn't need to survive process death. It's not more reliable — it's slower to start and more complex to debug. Use the simplest tool that fits.
Sudarshan Chaudhari
AI Systems Builder / Product Engineer
Bangkok, Thailand
Solo Android developer with 13+ years in QA, building Android apps, AI automation systems, and developer tools at SudarshanTechLabs.
Related Posts
Related Apps
Real-time family location sharing — Firebase Realtime DB for sub-second propagation, WorkManager + ForegroundService for OS-compliant background collection, geofencing via Google Maps API.
Building something? Available for Android dev and QA consulting.
Work with meComments — powered by Giscus
Real-time family location sharing — Firebase Realtime DB for sub-second propagation, WorkManager + ForegroundService for OS-compliant background collection, geofencing via Google Maps API.
ReadPrivate dream journal — structured entry capture, pattern tagging, and optional Claude-powered insight generation. All data stays on-device by default.
ReadWorkout tracker — exercise logging with set/rep/weight history, goal progression, and local Room DB persistence. No account, no cloud sync required.
Read