Skip to content

Instantly share code, notes, and snippets.

@robertohuertasm
Created June 30, 2019 11:32
Show Gist options
  • Save robertohuertasm/754c97179000e584bb9f5d517539922a to your computer and use it in GitHub Desktop.
Save robertohuertasm/754c97179000e584bb9f5d517539922a to your computer and use it in GitHub Desktop.
foreground_services
class StartReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED && getServiceState(context) == ServiceState.STARTED) {
Intent(context, EndlessService::class.java).also {
it.action = Actions.START.name
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
log("Starting the service in >=26 Mode from a BroadcastReceiver")
context.startForegroundService(it)
return
}
log("Starting the service in < 26 Mode from a BroadcastReceiver")
context.startService(it)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment