FirebaseJobDispatcher:何时调用JobService.onStopJob()

问题描述 投票:0回答:1

我已经看到有关此问题的问题。在完成工作后,签出了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()吗?

android firebase job-scheduling firebase-job-dispatcher
1个回答
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,这是一个“嘿,您必须停止”类调用。

© www.soinside.com 2019 - 2024. All rights reserved.