Firebase JobDispatcher Background Service

Android pass model Objects class from intent

We need background service for executing many task. For instance geo location, cloud messaging or to execute heavy task which cannot be done during android lifecycle. Some background service might not run for so long that’s why google created this library for doing long running task which is Firebase jobDispatcher.

What is a Firebase JobDispatcher or job Scheduler?

The JobScheduler is an Android system service available on API levels 21 (Lollipop)+. It provides an API for scheduling units of work (represented by jobservice subclasses) that will execute in your app’s process.

Why is this better than background services and listening for system broadcasts?

Running apps in the background is expensive, which is especially harmful when they’re not actively doing work that’s important to the user. That problem is multiplied when those background services are listening for frequently sent broadcasts (android.net.conn.CONNECTIVITY_CHANGE and android.hardware.action.NEW_PICTURE are common examples). Even worse, there’s no way of specifying prerequisites for these broadcasts. Listening for CONNECTIVITY_CHANGE broadcasts does not guarantee that the device has an active network connection, only that the connection was recently changed.

How to Integrate Firebase JobDispatcher Background Service?

So lets start. The official github repo is here and the implemented code is here

  • implement this code in app.gradle implementation ‘com.firebase:firebase-jobdispatcher:0.8.5’
  • add below code to manifest.
<service
android:name=".FirebaseJobDispatcher.DemoJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
  • add FirebaseJobDispatcher folder to your project.
  • initialize service on mainactivity to start the job.
void ScheduleJob(){

try {
FirebaseJobDispatcher jobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(MainActivity.this));

final Job.Builder builder = jobDispatcher
.newJobBuilder()
.setTag("Repetitive")
.setRecurring(false)
.setLifetime(Lifetime.FOREVER)
.setService(DemoJobService.class)
.setReplaceCurrent(false)
.setRetryStrategy(
jobDispatcher.newRetryStrategy(
RetryStrategy.RETRY_POLICY_EXPONENTIAL,
30,
3600));

builder.setConstraints(Constraint.ON_ANY_NETWORK,Constraint.DEVICE_IDLE);
builder.setTrigger(
Trigger.executionWindow(60, 60 + 50));
jobDispatcher.mustSchedule(builder.build());
}catch (Exception e){
e.printStackTrace();

}
}
  • By calling this you can start the job
  • And in DemoJobService ononStartJob you can define your job
  • I have created a notification service just for example you can create any other service with this.
DemoJobService
Sample notification

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

One thought on “Firebase JobDispatcher Background Service

Leave a Reply

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