android - FirebaseJobDispatcher:何时调用 JobService.onStopJob()
问题描述
我已经看到有关此事的问题。检查了源代码,但在完成工作后仍然无法弄清楚为什么JobService.onStopJob()
不被调用。
构造的代码Job
:
private Job jobFrom(Bundle bundle, int windowStart, int windowEnd) {
return dispatcher.newJobBuilder()
.setService(AttackJobService.class)
.setTag(attack.getPushId())
.setRecurring(false)
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
.setTrigger(Trigger.executionWindow(windowStart, windowEnd))
.setReplaceCurrent(false)
.setExtras(bundle)
.build();
}
一个工作安排如下:
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
dispatcher.mustSchedule(job);
我JobService
的还是很简单的,因为我还在尝试测试框架:
public boolean onStartJob(@NonNull JobParameters job) {
new Thread(() -> {
try {
Thread.sleep(2000);
jobFinished(job,false); //signal that the job is done
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
return true; // Answers to the question: "Is there still work going on?"
}
public boolean onStopJob(@NonNull JobParameters job) {
Log.d(TAG, "onStopJob() called");
return false; // Answers to the question: "Should this job be retried?"
}
被onStartJob()
调用并且线程开始执行。线程休眠 2 秒,然后jobFinished()
被调用。
这不意味着也onStopJob()
应该调用它吗?
解决方案
如果您阅读 的源代码public abstract class JobService extends Service {
,您可以阅读有关何时调用它的所有信息:
/**
* Called when the scheduling engine has decided to interrupt the execution of a running job, most
* likely because the runtime constraints associated with the job are no longer satisfied. The job
* must stop execution.
*
* @return true if the job should be retried
* @see com.firebase.jobdispatcher.JobInvocation.Builder#setRetryStrategy(RetryStrategy)
* @see RetryStrategy
*/
@MainThread
public abstract boolean onStopJob(JobParameters job);
这不是一个onJobStopped
回调,就像一个动画onComplete
,这是一个“嘿,你必须停止”的调用。
推荐阅读
- c# - Unity、C#、2D:如何将 UI 画布项(按钮)位置设置为与另一个游戏对象相同的位置?
- wordpress - 将可变产品价格范围替换为“来自:”+ WooCommerce 中使用 number_format 的最低价格
- java - Java接口和信息输出到控制台的问题
- python - 如何使用烧瓶在 html 中显示相同的缩进 json 内容
- spring-boot - Springboot GraphQL:错误的获取请求:路径不是模式'/json'
- arrays - C中二维字符数组的输入
- python - 第 2 行错误 python 速成课程 eric matthes 问题 9-2
- python - 如何使用图像的 RGBA 值列表在 Pygame 中显示图像
- functional-programming - 在 fp-ts 的管道中混合 Either 和 TaskEither
- c# - C# md5 哈希不匹配节点生成的 md5 哈希