Skip to content

Instantly share code, notes, and snippets.

@navczydev
Created October 28, 2021 23:08
Show Gist options
  • Save navczydev/58d0c52851ad9fd617a052ad839f19f9 to your computer and use it in GitHub Desktop.
Save navczydev/58d0c52851ad9fd617a052ad839f19f9 to your computer and use it in GitHub Desktop.
/**
* Used to demo the foreground service restrictions
*
*/
class BaseApplication : Application(), LifecycleEventObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_STOP -> {
Thread.sleep(5000)
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
this.startForegroundService(Intent(this, ForegroundService::class.java))
}
else -> {
this.startService(Intent(this, ForegroundService::class.java))
}
}
}
else -> {
// do nothing
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment