本文介绍了将上下文传递给JobService Android JobScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,将URL和Json存储在本地SQLite数据库中,然后安排JobService.

I am building an app where I store the url and Json in my local SQLite db and then schedule a JobService.

在JobService中,我从数据库中一一取出所有请求并执行它们.一旦执行它们,我只能在JobService中获得响应.现在,我真正的问题是如何将响应从服务发送回用户.

In the JobService, I take out all the requests one by one from the db and execute them. Once they are executed, I get the response inside the JobService only. Now my real problem is how do I send the response back to the user from the service.

我想到了在活动中实现回调/侦听器并在服务内部的侦听器对象中传递值.但是我正在从SQLite数据库中取出请求.因此,我不知道如何将回调实例或上下文保存在数据库中,这样我就无法在服务本身内部获得其余数据.

I thought of implementing a callback/listener in activity and passing the value in listener object inside the service. But I am taking out the requests from the SQLite db. So I don't know how to save the callback instance or the context inside my database so that I get that with the rest of the data inside the service itself.

有什么想法吗?

推荐答案

一种方法是使用事件总线实现:LocalBroadcastManager,greenrobot的EventBus,某些基于Rx的总线,甚至是简单的MutableLiveData单例.让服务在总线上发布消息.让UI层在总线上来回注册和注销.让UI层处理在总线上接收到的消息,如果UI层未接收到消息,则让服务引发Notification.

One approach is to use an event bus implementation: LocalBroadcastManager, greenrobot's EventBus, some Rx-based bus, or even a simple MutableLiveData singleton. Have the service post a message on the bus. Have your UI layer register and unregister from the bus as they come and go. Have the UI layer process messages received on the bus, and have the service raise a Notification if the UI layer does not pick up the message.

以下是使用以下示例的实现:

Here are sample implementations using:

  • LocalBroadcastManager: https://github.com/commonsguy/cw-omnibus/tree/v8.6/EventBus/LocalBroadcastManager
  • greenrobot's EventBus: https://github.com/commonsguy/cw-omnibus/tree/v8.6/EventBus/GreenRobot3
  • MutableLiveData: https://github.com/commonsguy/cw-androidarch/tree/v0.1/General/LiveBus

这篇关于将上下文传递给JobService Android JobScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:44