Last active
October 31, 2022 10:57
-
-
Save asissuthar/15c0fc085bc7a58cb0583134339fa86e to your computer and use it in GitHub Desktop.
This file contains 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
package com.asissuthar.experiments.service | |
import android.app.Service | |
import android.content.Intent | |
import android.os.IBinder | |
import com.asissuthar.experiments.common.AppEvent | |
import com.asissuthar.experiments.common.AppEventBus | |
import kotlinx.coroutines.* | |
import org.koin.android.ext.android.inject | |
class MyService : Service() { | |
private val appEventBus by inject<AppEventBus>() | |
private val scope = MainScope() + CoroutineName("MyService") | |
override fun onBind(intent: Intent?): IBinder? = null | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
scope.launch { | |
// Send event with data | |
appEventBus.emit(AppEvent.MyEventWithData(message = "Hello from Service!")) | |
// Send event without data for performing any action | |
appEventBus.emit(AppEvent.MyEventWithoutData) | |
} | |
return super.onStartCommand(intent, flags, startId) | |
} | |
override fun onDestroy() { | |
scope.cancel() | |
super.onDestroy() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment