Android - Firebase jobdispatcher [英] Android - Firebase jobdispatcher

查看:716
本文介绍了Android - Firebase jobdispatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用Firebase jobdispatcher安排网址命中并获取响应以更新数据库。
我希望在晚上每天运行一次。有谁知道这是可能的吗?

我找不到这样做的好例子。我已经阅读了android文档和 https://github.com / firebase / firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-

我需要使用Firebase jobdispatcher,因为我的目标是API 16 。

预先感谢。

更新

这是我每天安排一次的时间。

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

FirebaseJobDispatcher dispatcher =新的FirebaseJobDispatcher(新的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);
$ b $ if(result!= FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS){
Log.d(JOB_TAG,ERROR ON SCHEDULE);

$ / code $ / $ p

解决方案

Firebase JobDispatcher.A根据您的要求,您需要创建一个服务扩展JobService,从url获取响应并更新数据库。然后,您可以使用Firebase JobDispatcher安排此服务。在executionWindow中,您必须指定在理想情况下该作业应该运行的最早和最晚的时间。

如果您想安排作业每24小时后你可以使用执行窗口(60 * 60 * 24,60 * 60 * 24 + 60)。然后,如果你想要它应该每晚运行,那么你必须确保它是在夜间初始安排.For最初可以使用AlarmManager在安装应用程序时在夜间(仅一次)触发,并使用作业调度程序计划重复作业,或者另一种方式是基于与现在不同的方式和期望的执行时间,您可以使用非递归作业jobdispatcher,它将在晚上运行,并在该作业服务中,您可以使用作业调度程序安排重复作业。



ExecutionWindow指定大致的时间。它不能保证它会运行在给定的窗口。如果它错过了窗口,那么在理想情况下,作业将在最早的时间运行。对于重复作业,一旦作业完成,下一个作业将从上次作业的时间计算执行窗口时间。

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


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

解决方案

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

这篇关于Android - Firebase jobdispatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆