How to cast LifecycleOwner on Service class?

BadGuy :

I want to select data on room database with livedata from service class. How to cast the LifecycleOwner when observe it?

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());
    }
});
Birju Vachhani :

You can use LifecycleService like this:

Add this dependency to your app/build.gradle file:

dependencies {
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
}

Extend your service with LifecycleService:

class MyService extends LifecycleService {

    ...
}

After that you will be able to observe your LiveData.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=125097&siteId=1