【问题标题】:Firebase JobDispatcher-Scheduled jobs are lost on device rebootFirebase JobDispatcher-Scheduled 作业在设备重启时丢失
【发布时间】:2017-06-22 23:30:02
【问题描述】:

我正在使用Firebase-JobDispatcher。我已经安排了一些作业,如果我保持设备开启,它的工作正常。但是如果我重新启动我的设备,那么安排的作业不会执行或不会重新安排?我已使用setLifetime(Lifetime.FOREVER)。设备重启时仍会丢失作业。以下是我正在使用的代码-

Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("DataSend")
.setRecurring(false)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(0, 0))
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(Constraint.ON_ANY_NETWORK)
.setExtras(myExtrasBundle)
.build();

【问题讨论】:

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


【解决方案1】:

设置Lifetime.FOREVER后,您在AndroidManifest.xml文件中添加了以下权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

下面是安排工作的代码

Job job = jobDispatcher.newJobBuilder()
    .setService(MyJobService.class)
    .setTrigger(Trigger.executionWindow(windowStartTime, 3600))
    .setTag(PENDING_AUTH_JOB) //identifier for the job
    .setRecurring(false) // should not recur
    .setLifetime(Lifetime.FOREVER) // should persist device reboot
    .setReplaceCurrent(false) // no need to replace previous job
    .setConstraints(Constraint.ON_ANY_NETWORK) // set network availability constraint
    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
    .build();
try {
  jobDispatcher.mustSchedule(job);
} catch (FirebaseJobDispatcher.ScheduleFailedException e) {
  if (retryCount-- > 0) {
    scheduleJob(0);
  }
}

要检查的另一件事是未将执行窗口设置为0,0。始终将windowEnd 设置为大于windowStart

【讨论】:

  • 重启后是否满足约束?我的意思是手机是否连接到互联网来运行这项工作?
  • 是的,它已连接到互联网,如果我安排新作业,该作业将执行。仅当我重新启动设备作业丢失时
  • 我正在通过在安排作业时断开互联网连接并在设备重启后重新连接到互联网进行测试
  • 你能发布jobservice代码吗?只是想知道您如何决定是否执行作业
  • 我在 onStartJob 的开头放置了一个日志语句来检查它是否被调用。另外我正在我的工作服务中向服务器发送图像,基于这些事情我可以知道我的工作是执行与否
【解决方案2】:

我认为在您的 MyJobService 中应该返回 false 以便在执行后可以重新安排作业;

  public boolean onStartJob(final com.firebase.jobdispatcher.JobParameters jobParameters) {

        //Offloading work to a new thread.
        new Thread(new Runnable() {
            @Override
            public void run() {
                realm=Realm.getDefaultInstance();

                codeYouWantToRun(jobParameters);
            }
        }).start();

        return true;
    }



 public void codeYouWantToRun(final JobParameters parameters) {
 Log.d(TAG, "completeJob: " + "jobStarted");
//bla bla super code doing its linga linga ling 
                Log.d(TAG, "completeJob: " + "jobFinished");

                    //Tell the framework that the job has completed and doesnot needs to be reschedule. Set jobFinished false so that it can rescheduled on a change of network
                    jobFinished(parameters, false);
    }

【讨论】:

    【解决方案3】:

    试试setPersisted(boolean)方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2011-06-12
      • 1970-01-01
      相关资源
      最近更新 更多