我想从服务类中选择带有livedata的会议室数据库中的数据。观察时如何转换LifecycleOwner?
repositoryDatabase.getTimeline().observe(this, timelineList -> {
if (timelineList != null && timelineList.size() >= 10) {
JSONArray arrayTimeline = new JSONArray();
for (TimelineEntity timeline : timelineList) {
JSONObject objectTimeline = new JSONObject();
try {
objectTimeline.put("doku", timeline.getIdDokumen());
objectTimeline.put("entrydate", timeline.getEntryDate());
objectTimeline.put("lat", timeline.getLat());
objectTimeline.put("lng", timeline.getLng());
arrayTimeline.put(objectTimeline);
} catch (JSONException e) {
e.printStackTrace();
}
}
onUpdateLocation(arrayTimeline.toString());
}
});
最佳答案
您可以像这样使用LifecycleService
:
将此依赖项添加到您的 app / build.gradle 文件中:
dependencies {
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
}
使用
LifecycleService
扩展您的服务:class MyService extends LifecycleService {
...
}
之后,您将能够观察到
LiveData
。