Skip to content

Instantly share code, notes, and snippets.

@rinekri
Created January 24, 2019 16:58
Show Gist options
  • Select an option

  • Save rinekri/62f294e8681ece846b60d15f3da860ca to your computer and use it in GitHub Desktop.

Select an option

Save rinekri/62f294e8681ece846b60d15f3da860ca to your computer and use it in GitHub Desktop.
Bound service
class BoundService : Service() {
companion object {
private const val TAG = "BoundService"
fun newIntent(context: Context) = Intent(context, BoundService::class.java)
}
var message: String = "Message from bound service"
private val binder: BoundServiceBinder = BoundServiceBinder()
override fun onCreate() {
Log.e(TAG, "onCreate")
"$TAG: onCreate".showToast(applicationContext)
}
override fun onDestroy() {
Log.e(TAG, "onDestroy")
"$TAG: onDestroy".showToast(applicationContext)
}
override fun onBind(intent: Intent?): Binder {
Log.e(TAG, "onBind")
"$TAG: onBind".showToast(applicationContext)
return binder
}
override fun onUnbind(intent: Intent?): Boolean {
Log.e(TAG, "onUnbind")
"$TAG: onUnbind".showToast(applicationContext)
return false
}
inner class BoundServiceBinder : Binder() {
val service: BoundService
get() = this@BoundService
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment