I'm playing a bit with Android and writing a small app with an activity and a service. The activity starts the service on button click and it should fetch some data from a URI and return it as a result. Sometimes, data is not available or doesn't meet a criteria, and then the service should retry every X minutes even if the activity is in the background.
I've implemented the communication between the activity and the service via Intent
and ResultReceiver
(passed in the bundle). When implementing the service, I wanted to use the new JobScheduler
component but then saw it can only accept a PersistableBundle
which can't add a Parcelable
object (ResultReceiver
) but only the basic types.
I want the service to schedule a job that will run when network is available and every X minutes to check for data. Once it gets it, I want the data to be returned to the service so it can decide if it's acceptable or we need to retry again. If the service accepts the data, it will return it to the activity via the ResultsReceiver
.
I can't use ResultsReceiver
with the JobService
, and it since I don't construct the JobService
instance (done by the JobInfo.Builder
), I can't pass it a reference to a callback object.
How can I communicate between a scheduled job and the service that invoked it?
Sorry if it's trivial, I'm not familiar with Android..
Thanks!
JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory. So if you want to perform some background job like some network operations no matter your app is running or not.
It's a new type of service, that is invoked for tasks that are scheduled to be run depending on system conditions (e.g. idle, plugged in). Entry point for the callback from the JobScheduler. This is the base class that handles asynchronous requests that were previously scheduled.
To create a Job Service, start by extending the JobService class and overriding 'onStartJob' and 'onStopJob'. OnStartJob is called by the Android system when it starts your job. If your task is short & simple,implement the logic directly in onStartJob() and return false when you are finished.
OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).
How can I communicate between a scheduled job and the service that invoked it?
Generally, you don't. Neither your service nor your activity are likely to exist. The point behind JobScheduler
is to have jobs run when your app is no longer running. If you are using it in other ways, that may not be appropriate.
That being said, you are welcome to use an event bus (greenrobot's EventBus would be my choice, though LocalBroadcastManager
would work here as well), to raise an event within your process to say that the job is completed. Your service (or activity) could register for events on the bus and react when those events are raised. Yet, at the same time, event buses are perfectly content to let events "fall on the floor" if there are no registered handlers, so your JobService
can just raise the event in a "fire and forget" mode if desired.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With