android–Firebase jobdispatcher不在指定的窗口内触发

android–Firebase jobdispatcher不在指定的窗口内触发,第1张

概述我正在实施FirebaseJobdispatcher,触发时间指定在10到20秒之间.这是我安排工作的代码:publicstaticvoidscheduleCompatibleJob(Contextcontext){FirebaseJobDispatcherdispatcher=newFirebaseJobDispatcher(newGooglePlayDriver(context));JobmyJob=dispa

我正在实施Firebase Jobdispatcher,触发时间指定在10到20秒之间.这是我安排工作的代码:

public static voID scheduleCompatibleJob(Context context) {    FirebaseJobdispatcher dispatcher = new FirebaseJobdispatcher(new GooglePlayDriver(context));    Job myJob = dispatcher.newJobBuilder()            .setService(DataTrackerJobService.class) // the JobService that will be called            .setTag("API_trigger")            .setRecurring(true)            .setlifetime(lifetime.FOREVER)            .setTrigger(Trigger.executionWindow(10, 20)) // 10 to 20 seconds            .setReplaceCurrent(true)            .setRetryStrategy(RetryStrategy.DEFAulT_liNEAR)            .setConstraints(Constraint.ON_ANY_NETWORK)            .build();    dispatcher.mustSchedule(myJob);}

和服务类:

public class DataTrackerJobService extends JobService {@OverrIDepublic boolean onStartJob(final JobParameters job) {    Log.e("start job", "started " + job);    (new Handler()).postDelayed(new Runnable() {        @OverrIDe        public voID run() {            Log.e("handler", "run");            jobFinished(job, true);        }    }, 2000);    return true;}@OverrIDepublic boolean onStopJob(JobParameters job) {    Log.e("stop job", "stopped " + job);    return false;}

}

作业调度程序正在运行,但logcat中的时间不正确.随着工作的每次重新安排,时间差距不断增加,并且从未在10到20秒之间.

06-07 11:19:16.429 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f4250de4
06-07 11:19:18.432 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:20:16.436 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f16ceb31
06-07 11:20:18.438 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:21:58.519 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f4c635cd
06-07 11:22:00.520 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:23:40.602 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f15f8e29
06-07 11:23:42.605 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:25:52.642 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f48c1045
06-07 11:25:54.644 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:28:52.652 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@f3b49821
06-07 11:28:54.655 26174-26174/com.example.jobdispatcher E/handler: run
06-07 11:32:04.688 26174-26174/com.example.jobdispatcher E/start job: started com.firebase.jobdispatcher.JobInvocation@e7f3c1bd
06-07 11:32:06.690 26174-26174/com.example.jobdispatcher E/handler: run

请在logcat中查看时间.指导我在哪里出错,或者它应该只以这种方式工作?基本上我想实现它24小时的时间间隔,但我想知道这是否正常工作然后在触发时间指定的两倍时间之后将调用作业.

解决方法:

如果您查看Trigger类的来源,您会注意到您无法确定您的作业是否会在给定时间运行.

/** * Creates a new ExecutionWindow based on the provIDed time interval. * * @param windowstart The earlIEst time (in seconds) the job should be *                    consIDered eligible to run. Calculated from when the *                    job was scheduled (for new jobs) or last run (for *                    recurring jobs). * @param windowEnd   The latest time (in seconds) the job should be run in *                    an IDeal world. Calculated in the same way as *                    {@code windowstart}. * @throws IllegalArgumentException if the provIDed parameters are too *                                  restrictive. */public static JobTrigger.ExecutionWindowTrigger executionWindow(int windowstart, int windowEnd) {    if (windowstart < 0) {        throw new IllegalArgumentException("Window start can't be less than 0");    } else if (windowEnd < windowstart) {        throw new IllegalArgumentException("Window end can't be less than window start");    }    return new JobTrigger.ExecutionWindowTrigger(windowstart, windowEnd);}

您还应注意,下一个重复工作仅在前一个重复工作结束时才开始.因此,当您的工作耗时且昂贵时,下一个执行时间可能会出乎意料.对于耗时的工作,您应该扩展SimpleJobService.

为了创建重复的任务,我使用我的util方法创建一个合适的Trigger:

public class JobdispatcherUtils {    public static JobTrigger periodicTrigger(int frequency, int tolerance) {        return Trigger.executionWindow(frequency - tolerance, frequency);    }}

用法:

class DataTrackerJobService extends SimpleJobService {    // ...}public static voID scheduleCompatibleJob(Context context) {    FirebaseJobdispatcher dispatcher = new FirebaseJobdispatcher(new GooglePlayDriver(context));    Job myJob = dispatcher.newJobBuilder()            .setService(DataTrackerJobService.class) // the JobService that will be called            .setTag("API_trigger")            .setRecurring(true)            .setlifetime(lifetime.FOREVER)            .setTrigger(JobdispatcherUtils.periodicTrigger(20, 1)) // repeated every 20 seconds with 1 second of tollerance            .setReplaceCurrent(true)            .setRetryStrategy(RetryStrategy.DEFAulT_liNEAR)            .setConstraints(Constraint.ON_ANY_NETWORK)            .build();    dispatcher.mustSchedule(myJob);}
总结

以上是内存溢出为你收集整理的android – Firebase jobdispatcher不在指定的窗口内触发全部内容,希望文章能够帮你解决android – Firebase jobdispatcher不在指定的窗口内触发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111342.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存