BackgroundMode

sealed class BackgroundMode(source)

Describes which background execution strategy fits your use case.

ModeSurvives app killVisible to userBest for
Coroutine❌❌Short async work while app is open
WorkManagerOnce✅❌Deferred one-shot tasks
WorkManagerPeriodic✅❌Recurring background sync
WorkManagerScheduled✅❌Calendar-based (every Friday 7PM)
Foreground✅✅ (notif)Music, GPS, long downloads

Inheritors

Constructors

Link copied to clipboard
protected constructor()

Types

Link copied to clipboard
data class Coroutine(val scope: CoroutineScope, val dispatcher: CoroutineDispatcher = Dispatchers.IO) : BackgroundMode

Coroutine tied to a CoroutineScope — cancelled when scope is cancelled.

Link copied to clipboard
data class Foreground(val channelId: String, val channelName: String, val notificationId: Int = 1, val title: String, val text: String = "", @DrawableRes val icon: Int) : BackgroundMode

Foreground service with a visible notification — survives app kill.

Link copied to clipboard
data class WorkManagerOnce(val uniqueName: String, val policy: ExistingWorkPolicy = ExistingWorkPolicy.KEEP, val constraints: Constraints = Constraints.NONE, val inputData: Map<String, Any> = emptyMap()) : BackgroundMode

WorkManager one-time task — survives app kill, runs once.

Link copied to clipboard
data class WorkManagerPeriodic(val uniqueName: String, val intervalMinutes: Long, val policy: ExistingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP, val constraints: Constraints = Constraints.NONE, val inputData: Map<String, Any> = emptyMap()) : BackgroundMode

WorkManager periodic task — survives app kill, repeats on interval.

Link copied to clipboard
data class WorkManagerScheduled(val uniqueName: String, val dayOfWeek: DayOfWeek, val hour: Int, val minute: Int = 0, val policy: ExistingWorkPolicy = ExistingWorkPolicy.REPLACE, val constraints: Constraints = Constraints.NONE) : BackgroundMode

WorkManager calendar-scheduled task — runs at a specific day/time, self-reschedules.