Using Firebase JobDispatcher For Efficient Job Scheduling In Android

Posted By : Daljeet Singh | 31-May-2018

Scheduling and running an asynchronous task after a specific interval of time or after the occurrence of a specific event is sometimes one of the most fundamental requirements of an Android application. In a majority number of cases, these tasks are to be performed outside the direct flow of user interaction. To accomplish this functionality, the Android framework provides us with a number of alternatives, out of which, a couple of popular ones are :

  • Alarm Manager - The alarm manager provides you access to the alarm services of the system. The alarm services help your application run a task at a specified point in time in the future. When an alarm is triggered, its Intent is broadcasted by the system, resulting in the automatic start of the application if it was not previously running. The alarm manager holds a wake lock of the CPU for the duration of the execution of its onReceive() method which essentially prevents the phone from sleeping until the broadcast is handled.

  • JobScheduler - Jobscheduler is the Android framework's API for task scheduling. The jobscheduler executes tasks/jobs inside the process of your own application.Jobscheduler allows you to create JobInfo objects and pass them to the jobscheduler using schedule(JobInfo). When the specified conditions are met, the system executes the job on the application's JobService.The component that implements the logic for a job is mentioned while creating the JobInfo object using JobInfo.Builder(int,android.content.ComponentName).

Although alarm manager and job scheduler both provide intelligent ways of scheduling jobs in android, each of these methods has its own limitations :

  • The AlarmManager API can only be used for jobs that execute at a given point in time. It cannot be used to trigger jobs at pre-requisite conditions such as connecting the charger or network availability.

  • The JobScheduler provides support for executing tasks based on specified conditions, however a major drawback with JobScheduler is that it can only be used for API level>=21, hence, if your application's minimum SDK level is lower than 21,JobScheduler will not be the required solution.

To overcome the aforementioned challenges, we can make use of the FirebaseJobDispatcher inside our app. The JobDispatcher offers a JobScheduler compatible API which works on all the recent versions of Android(API level 14+). The Firebase JobDispatcher internally makes use of the Android framework's JobScheduler API in case of API level >=21 and uses the AlarmManager for API level lower than 21.

You can add the Firebase JobDispatcher to your project by adding the following dependency :

implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

Here is a very basic jobservice :

public class MyJobService extends JobService {
    @Override
    public boolean onStartJob(JobParameters job) {
        // Do some work here

        return false; 
    }

    @Override
    public boolean onStopJob(JobParameters job) {
        return false;
    }
}

You need to add the service to the manifest :

<service
    android:exported="false"
    android:name=".MyJobService">
    <intent-filter>
        <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
    </intent-filter>
</service>

Create a dispatcher using the Google play driver :

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

You can schedule the job by writing the following piece of code :

Job myJob = dispatcher.newJobBuilder()
    .setService(MyJobService.class) 
    .setTag("job-tag")
    .build();

dispatcher.mustSchedule(myJob);

 

About Author

Author Image
Daljeet Singh

Daljeet has experience developing android applications across a range of domains such as Cryptocurrency, Travel & Hotel Booking, Video Streaming and e-commerce. In his free time, he can be found playing/watching a game of football or reading up on either

Request for Proposal

Name is required

Comment is required

Sending message..