Firebase jobdispatcher 未在指定 window 内触发
Firebase jobdispatcher not triggering within specified window
我正在实施 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);
}
和服务 class:
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 秒之间。
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 小时的时间间隔内实现它,但我想知道这是否可行,然后在触发时间中指定的时间加倍后调用一次作业。
如果您查看触发器 class 的来源,您会发现您不确定您的作业是否会在给定时间 运行。
/**
* 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 方法来创建一个合适的触发器:
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,触发时间指定在 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);
}
和服务 class:
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 秒之间。
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 小时的时间间隔内实现它,但我想知道这是否可行,然后在触发时间中指定的时间加倍后调用一次作业。
如果您查看触发器 class 的来源,您会发现您不确定您的作业是否会在给定时间 运行。
/**
* 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 方法来创建一个合适的触发器:
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);
}