Welcome to the android tutorial.
In this post, we talk about JobSchedular. A job scheduler is more effective than alarm manager. Want to see it benefits then check out this link.
For scheduling, you have many options to use.
1. Android Alarm manager
2. Job Scheduler
3. Gcm Network Manager
In the previous post, we talk about android alarm manager. Are you confused about those? which one is appropriate for your android app. Read this document Become a pro at scheduling tasks in Android
Now come to code- Before starting we have to add below line in the build.gradle file
1. onStartJob()
2. onStopJob()
onStartJob() will call when the job is started. So we need to implement the start logic in this methods.
onStopJob() will call after job will finish. In my case, I show a notification on start Job.
Full Java code of service class-
1. UNTIL_NEXT_BOOT
2.FOREVER
1. DEVICE_CHARGING -> the will be run only while the device is running.
2. ON_ANY_NETWORK -> for this case job will be running on any network
3. ON_UNMETERED_NETWORK-> on unmetered connection.
In my case, I set on any network
set trigger for the job to start by calling setTrigger and the parameter will be another method Trigger.executionWindow(). This method has two parameters.The first argument for * which the data should be synced Note: this end time is not * guaranteed, but is more of a guideline for FirebaseJobDispatcher to go off we need two time interval time and flex time for that add those below line-
oh! we done. Now I think you can use this job scheduler in your next android project.
That's it's today post
Happy coding.
Android |
In this post, we talk about JobSchedular. A job scheduler is more effective than alarm manager. Want to see it benefits then check out this link.
For scheduling, you have many options to use.
1. Android Alarm manager
2. Job Scheduler
3. Gcm Network Manager
In the previous post, we talk about android alarm manager. Are you confused about those? which one is appropriate for your android app. Read this document Become a pro at scheduling tasks in Android
Now come to code- Before starting we have to add below line in the build.gradle file
compile 'com.firebase:firebase-jobdispatcher:0.5.0'Now we need to a service class. Create a services class and extend it to JobService. In this case, my class name is EverydayJobTask.java
public class EverydayJobTask extends JobService { }we need to override two class-
1. onStartJob()
2. onStopJob()
onStartJob() will call when the job is started. So we need to implement the start logic in this methods.
onStopJob() will call after job will finish. In my case, I show a notification on start Job.
Full Java code of service class-
public class EverydayJobTask extends JobService { @Override public boolean onStartJob(JobParameters job) { //if job schedule then set a notification TaskNotification.notify(EverydayJobTask.this, "Start sync Task... ", 0); return true; } @Override public boolean onStopJob(JobParameters job) { return true; } }Now we need to build FirebaseJobDispatcher. we need to create a google play driver
Driver driver = new GooglePlayDriver(context);now create a firebase job dispatcher variable and pass this driver
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);next step is to create a job this job will help to do the task with time-
Job syncJob = dispatcher.newJobBuilder()set service to the job by calling setService and parameter will be a service class and its super class must be JobService.
.setService(EverydayJobTask.class)set a tag and this tag must be unique. this tag is used to identify this job.
.setTag("Everyday_Task")set it's lifetime by calling setLifeTime(). setLifetime sets how long this job should persist you two option
1. UNTIL_NEXT_BOOT
2.FOREVER
.setLifetime(Lifetime.FOREVER)set a Network constraints on which this Job should run. You have some option
1. DEVICE_CHARGING -> the will be run only while the device is running.
2. ON_ANY_NETWORK -> for this case job will be running on any network
3. ON_UNMETERED_NETWORK-> on unmetered connection.
In my case, I set on any network
.setConstraints(Constraint.ON_ANY_NETWORK)Note: It might be a good idea to include a preference for this, as some users may not want to download any data on their mobile plan.
set trigger for the job to start by calling setTrigger and the parameter will be another method Trigger.executionWindow(). This method has two parameters.The first argument for * which the data should be synced Note: this end time is not * guaranteed, but is more of a guideline for FirebaseJobDispatcher to go off we need two time interval time and flex time for that add those below line-
private static final int HOURS = 3; private static final int INTERVAL_SECONDS = (int) TimeUnit.HOURS.toSeconds(HOURS); private static final int FLEXTIME_SECONDS = INTERVAL_SECONDS / 3;Note: I set interval time three hours. If you want to set more hour then change hours variable. After that add below code-
.setTrigger(Trigger.executionWindow(INTERVAL_SECONDS,INTERVAL_SECONDS+FLEXTIME_SECONDS))If you want to set this job requiring the set true in the setRecurring or don't set false-
.setRecurring(true)once the job is finished then call build method. this method has returned a job.
.build();we finished making the job. But we have one more step to finish. we need to set schedule this job with the dispatcher.
dispatcher.schedule(syncJob);
oh! we done. Now I think you can use this job scheduler in your next android project.
That's it's today post
Happy coding.