我有旧的Android/java代码,其中包含两个从IntentService
派生的代码,并且这些服务而不是在单独的进程中运行。
问题是如何从这些IntentService
返回结果。
使用Handler
+ Runnable
返回一个服务结果,在主循环中运行代码:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
MyApplication.get().setFoo(someThing);
}
});
另一种是使用LocalBroadcastManager.getInstance(this).sendBroadcast(in);
向Activity
发送消息,Activity
在onResume
中通过BroadcastReceiver
订阅消息,在onPause
中通过取消订阅。
我说的对吗?在这两种情况下,都可以使用LiveData
来简化事情?
IntentService
应该创建LiveData
,谁应该observe
它,当新数据到达时,IntentService
应该调用postValue
,或者可能有一些珊瑚礁阻止了LiveData
的使用?
https://stackoverflow.com/questions/46207758
复制相似问题