gpt4 book ai didi

java - 如何将 LifecycleOwner 转换到服务类上?

转载 作者:太空狗 更新时间:2023-10-29 14:35:23 25 4
gpt4 key购买 nike

我想使用来自服务类的实时数据选择房间数据库中的数据。如何在观察时转换 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

关于java - 如何将 LifecycleOwner 转换到服务类上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57229877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com