FirebaseJobDispatcher:何时调用JobService.onStopJob()

时间:2019-02-23 00:18:54

标签: android firebase job-scheduling firebase-job-dispatcher

我已经看到有关此事的问题。在完成工作后,检出了source code,但仍然找不到为什么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()吗?

1 个答案:

答案 0 :(得分:0)

如果您阅读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一样,这是“嘿,您必须停止”类调用。