Are there any cons of using Kotlin state flow in companion object for checking if background service is running?
I wanted to use ActivityManager and its function isServiceRunning(Service::class.name), but this method is deprecated.
By the way, I should be observing this service from Fragment/Fragment's ViewModel, so I wanted to avoid Binding the service to Activity.
class TestService : Service() {
companion object {
private val _testStateFlow = MutableStateFlow<TestServiceEvent>()
val testServiceStateFlow = _testStateFlow.asStateFlow()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
_testStateFlow.value = TestServiceEvent.ServiceRunning(true)
....
....
....
....
_testStateFlow.value = TestServiceEvent.TestTaskDone()
}
override fun onDestroy(){
super.onDestroy()
_testStateFlow.value = TestServiceEvent.ServiceRunning(false)
}
sealed class TestServiceEvent{
data class ServiceRunning(val isRunning : Boolean) : TestServiceEvent()
object TestTaskDone : TestServiceEvent()
}
}