Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Firebase jobdispatcher

I would like to know if it's possible to use Firebase jobdispatcher to schedule an url hit and get the response in order to update the db. I would like it to run once per day at night. Does anyone know if this is possible?

I can't find any good example of doing this. I already read android documentation and https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher- .

I need to use Firebase jobdispatcher because I'm targeting API 16.

Thanks in advance.

UPDATE

This is what I did to schedule it once per day.

final int periodicity = (int) TimeUnit.HOURS.toSeconds(24);
final int toleranceInterval = (int) TimeUnit.HOURS.toSeconds(1);

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job job = dispatcher.newJobBuilder()
        .setService(UpdateTVJobService.class)
        .setTag(JOB_TAG)
        .setTrigger(Trigger.executionWindow(periodicity, periodicity + toleranceInterval))
        .setLifetime(Lifetime.FOREVER)
        .setRecurring(true)
        .setReplaceCurrent(true)
        .build();

int result = dispatcher.schedule(job);

if (result != FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS) {
    Log.d("JOB_TAG", "ERROR ON SCHEDULE");
}
like image 401
Sol Avatar asked Feb 01 '17 20:02

Sol


Video Answer


2 Answers

You can schedule recurring jobs using Firebase JobDispatcher.As per your requirement,you need to create a service extending JobService that get response from url and update the db . Then you can schedule this service using Firebase JobDispatcher .In executionWindow you have to specify the earliest and latest time that job should run in ideal circumstances.

If you want to schedule job after every 24 hours you can use execution window (60*60*24,60*60*24+60).Then if you want that it should run every night then you have to make sure that it is initially scheduled at night.For that you can initially use AlarmManager to be fired at night(only once) when app is installed and schedule a recurring job using job dispatcher OR another way is that based on difference from now and the desired execution time you can schedule a non recursive job using jobdispatcher which will run at night and inside that job service you can schedule recurring job using job dispatcher .

ExecutionWindow specifies approximate time. It's not guaranteed it would run at the given window. If it misses the window the job will run at earliest time later under ideal circumstances.For recurring jobs once the job has finished next job will calculate execution window time from the time job last run.

Job myJob = dispatcher.newJobBuilder()
                    .setTag("my-unique-tag")
                    .setRecurring(true)
                    .setLifetime(Lifetime.FOREVER)
                    .setService(MyJobService.class)
                    .setTrigger(Trigger.executionWindow(60*60*24,60*60*24+60))
                    .setReplaceCurrent(false)
                    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                    .setConstraints(Constraint.ON_ANY_NETWORK)
                    .setExtras(myExtrasBundle)
                    .build();

dispatcher.mustSchedule(myJob);
like image 149
Android Developer Avatar answered Oct 24 '22 01:10

Android Developer


You can schedule a recurring job by telling the Job.Builder to create a recurring job with a Trigger that has an execution window according to your needs.

like image 28
Doug Stevenson Avatar answered Oct 24 '22 02:10

Doug Stevenson