Skip to content

Instantly share code, notes, and snippets.

@diefferson
Last active November 23, 2018 13:57
Show Gist options
  • Select an option

  • Save diefferson/72c537147a39454045dde1861c95abcc to your computer and use it in GitHub Desktop.

Select an option

Save diefferson/72c537147a39454045dde1861c95abcc to your computer and use it in GitHub Desktop.
Start Service Android O
fun startService(context: Context){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(Intent(context, MyService::class.java))
} else {
context.startService(Intent(context, MyService::class.java))
}
}catch (e :Exception){
e.stackTrace
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(MyService.SERVICE_NOTIFICATION_ID, getNotificationService())
}
doAnything()
return super.onStartCommand(intent, flags, startId)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment