Firebase JobDispatcher

If you have some heavy work to do and it doesn’t need to be done right now, then I’m going to suggest you use to use Firebase JobDispatcher.It is guaranteed to get your job done.It operates at the System level, it can use several factors to intelligently schedule your background work to run with the jobs from other apps as well, that means we can minimize things like radio use, which is a clear battery win.API 24, JobScheduler even considers memory pressure, which is a clear overall win for devices and their users.It doesn’t perform work solely based on time, but rather based on conditions.For example, you can use setRequiredNetworkType for jobs.
That you want to execute when the user is on a metered vs an unmetered connection or you can call setLifeTime for jobs that you want to persist across a potential reboot.

Getting started


Add the following to your app build.gradle‘s dependencies

 Create Job

You can define conditions when you are creating the job through the Job object.To build that Job object you need two things every time, and then the criteria are all the bonus that’s over here.But you need a job tag to help you distinguish which job this and a job service.

Create a new JobService

Your job service is actually going to be a service that extends the JobService class, and this is where you’ll define the work that you’ll be doing.You will need to implement a few required methods.

onStartJob is called by the system when it is time for your job to execute.This is where the one tricky part about JobSchedulaer exists.Your JobService runs on the main thread.So use onStartJob to either perform simple work And if you do kick off something else, you’ll need to return true.
But if you’re done with everything, go ahead and return false.This will let the system know whether your job has any ongoing work still.

onStopJob is called by the system if your job is canceled before being finished, perhaps because the conditions are no longer being met like the device has been unplugged.So use this for safety checks and clean up, and then return true if you’d like the system to reschedule the job or false if it doesn’t matter and the job will be dropped.

jobFinished is not a method you override, and the system won’t call it, but that’s because you need to be one to call this method once your service or thread has finished working on the job.That is if your onStartJob returned true because this is how the system knows that your work is really done and it can release your wake-lock.So if you forget your app is going to look pretty guilty in the battery stats line up.jobFinished take two parameters, the current job, so that it knows which one we are talking about and a boolean indicating whether you’d like to reschedule the job.Perhaps your work failed for some reason.So this will kick off the JobScheduler’s essential backoff logic for you or else the logic you specified in Job.

Now, as with any service, you’ll need to add this one to your AndroidManifest.xml. What’s different, though, is that you need to add a permission that will allow JobScheduler to call your jobs and be the only one that can access your jobService.

Finally, you can schedule a job using FirebaseJobDispatcher, which you will get from the system, then call schedule using that super perfect JobInfo object you create, and you are good to go.


There are a lot of pieces to be sure, and you’ll need to think carefully about when and what should trigger your job and what happens if it fails for some reason.But overall FirebaseJobDispatcher was designed to be easy to work with.So give it a try and go build batter apps.

Leave a Reply

Your email address will not be published. Required fields are marked *