Firebase JobDispatcher未在指定窗口内触发

编程入门 行业动态 更新时间:2024-10-09 23:22:08
本文介绍了Firebase JobDispatcher未在指定窗口内触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am implementing Firebase Jobdispatcher with trigger time specified between 10 to 20 seconds. This is my code to schedule a job:

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 { @Override public 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; } @Override public boolean onStopJob(JobParameters job) { Log.e("stop job", "stopped " + job); return false; }

}

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

Job dispatcher is running, but the time in logcat is not correct. With every rescheduling of job, the time gap keeps on increasing, and was never in between 10 to 20 seconds.

06-07 11:19:16.429 26174-26174/com.example.jobdispatcher E/开始工作:开始com.firebase.jobdispatcher.JobInvocation@f4250de4 06-07 11:19:18.432 26174-26174/com.example.jobdispatcher E/handler:运行 06-07 11:20:16.436 26174-26174/com.example.jobdispatcher E/开始工作:开始com.firebase.jobdispatcher.JobInvocation@f16ceb31 06-07 11:20:18.438 26174-26174/com.example.jobdispatcher E/handler:运行 06-07 11:21:58.519 26174-26174/com.example.jobdispatcher E/开始工作:开始com.firebase.jobdispatcher.JobInvocation@f4c635cd 06-07 11:22:00.520 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:23:40.602 26174-26174/com.example.jobdispatcher E/开始工作:开始com.firebase.jobdispatcher.JobInvocation@f15f8e29 06-07 11:23:42.605 26174-26174/com.example.jobdispatcher E/handler:运行 06-07 11:25:52.642 26174-26174/com.example.jobdispatcher E/开始工作:开始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/开始工作:开始com.firebase.jobdispatcher.JobInvocation@f3b49821 06-07 11:28:54.655 26174-26174/com.example.jobdispatcher E/handler:运行 06-07 11:32:04.688 26174-26174/com.example.jobdispatcher E/开始工作:开始com.firebase.jobdispatcher.JobInvocation@e7f3c1bd 06-07 11:32:06.690 26174-26174/com.example.jobdispatcher E/handler:运行

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小时的时间间隔内实现它,但是我想知道这是否可以正常工作,然后一次在触发时间中指定的两倍时间后将调用作业.

Please check time in logcat. Guide me where am I going wrong in this, or is it supposed to work this way only? Basically I want to implement it for a time gap of 24 hours, but I wonder if this is the working then at one time job will be invoked after double the time specified in the trigger time.

推荐答案

如果查看Trigger类的来源,您会发现不确定自己的作业是否会在给定的时间运行.

If you look at source of Trigger class you'll notice that you have no sure that your job will run at given time.

/** * 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.

You should also note that the next repetitive work is started only when the previous one is over. So when your work is long and expensive, the next execution time may be unexpected. For time consuming jobs you should extend SimpleJobService.

为了创建重复的任务,我使用了创建适当触发器的util方法:

For creating a repeated task I am using my util method that creates a proper 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); }

更多推荐

Firebase JobDispatcher未在指定窗口内触发

本文发布于:2023-11-27 14:24:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   Firebase   JobDispatcher

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!