基本上我想知道服务是否正在运行,并使用LiveData观察其状态,而无需先绑定到服务。 您如何看待以下代码:
class MyService : Service() {
val state = ServiceState.state
...
// update state
}
class ServiceState(...) {
...
}
object ServiceState {
val state = MutableLiveData<ServiceState>()
}
class MyViewModel : ViewModel() {
val serviceState: LiveData<ServiceState> = ServiceState.state
...
}