Android:将服务状态存储在单例LiveData中是一个好主意吗?

时间:2019-05-10 13:27:57

标签: android service state android-livedata

基本上我想知道服务是否正在运行,并使用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
    ...
}

0 个答案:

没有答案