Skip to content

Instantly share code, notes, and snippets.

@Kvest
Created May 5, 2019 11:22
Show Gist options
  • Save Kvest/a9d6fcd34b15f83ed5cfc66d4110e12b to your computer and use it in GitHub Desktop.
Save Kvest/a9d6fcd34b15f83ed5cfc66d4110e12b to your computer and use it in GitHub Desktop.
class TService : Service() {
companion object {
fun start(context: Context, id: Int) {
val intent = Intent(context, TService::class.java)
intent.putExtra("id", id)
context.startService(intent)
}
}
override fun onCreate() {
super.onCreate()
Log.d("TService", "create service")
}
override fun onDestroy() {
super.onDestroy()
Log.d("TService", "destroy service")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val id = intent?.getIntExtra("id", -1) ?: -2
Log.d("TService", "onStartCommand id=$id startId=$startId")
val timeout = when(id) {
0 -> 1_000L
1 -> 4_000L
2 -> 2_000L
else -> 10_000L
}
Handler().postDelayed({
Log.d("TService", "stopSelf id=$id startId=$startId ")
stopSelfResult(startId)
}, timeout)
return START_STICKY
}
override fun onBind(intent: Intent?) = null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment