Skip to content

Instantly share code, notes, and snippets.

@Struka9
Created August 13, 2019 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Struka9/a009f1f42cc9d33376e2809d570c183e to your computer and use it in GitHub Desktop.
Save Struka9/a009f1f42cc9d33376e2809d570c183e to your computer and use it in GitHub Desktop.
// MyStartedForegroundService.kt
class MyStartedForegroundService: Service() {
private val NOTIFICATION_ID = 12345
private fun createNotification(): Notification {
//...
}
private fun doWork(startId: Int, url: String) {
startForeground(NOTIFICATION_ID, createNotification())
// Let's fetch something using the url
stopSelf(startId)
}
override fun (intent: Intent?, flags: Int, startId: Int): Int {
intent?.getStringExtra("EXTRA_URL")?.let {
doWork(startId, it)
}
return START_REDELIVER_INTENT
}
override fun onBind(intent: Intent): IBinder? {
// If we don't need to support binding for the service we return a null IBinder
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment