Android - Firebase jobdispatcher

空扰寡人 提交于 2019-11-27 19:52:05

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!