Created
January 24, 2019 16:58
-
-
Save rinekri/62f294e8681ece846b60d15f3da860ca to your computer and use it in GitHub Desktop.
Bound service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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