1

我已经尝试了evernote-android-job中的所有以下方法。似乎 schedulePeriodicJob 方法在某个时间间隔内运行,而 scheduleAdvancedJob 方法只运行一次。

通过一些示例帮助我如何设置 setExecutionWindow、setExact 和 setPeriodic 的值?

private void scheduleAdvancedJob() {
    PersistableBundleCompat extras = new PersistableBundleCompat();
    extras.putString("key", "Hello world");

    int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setExecutionWindow(30_000L, 40_000L)
            .setBackoffCriteria(5_000L, JobRequest.BackoffPolicy.EXPONENTIAL)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(false)
            .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
            .setExtras(extras)
            .setRequirementsEnforced(true)
            .setUpdateCurrent(true)
            .build()
            .schedule();
}

private void schedulePeriodicJob() {
    int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5))
            .build()
            .schedule();
}

private void scheduleExactJob() {
    int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setExact(20_000L)
            .build()
            .schedule();
}
4

1 回答 1

0

我遇到过同样的场景并使用了这个并获得了成功。你也可以试试。

public class RunFSJob extends Job {

    public static final String TAG = "JOB_FOR_START_SIGNARSERVICE";

    @Override
    @NonNull
    protected Result onRunJob(@NonNull Params params) {

        try {

            deleteCache(getContext().getApplicationContext());

            getContext().getApplicationContext().startService(new Intent(getContext().getApplicationContext(), SignalRService.class));

            Log.v("schedulePeriodicJob(15)", "JOB Done");

            cancel();

            Log.v("schedulePeriodicJob(15)", "JOB cancled");

            schedulePeriodicJob();

            Log.v("schedulePeriodicJob(15)", "JOB scheduled again from RunFSJob");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Job.Result.SUCCESS;
    }
}
  • 首先使用定期作业并每次从 onRunJob() 方法安排相同的作业。你也必须取消上一份工作。

  • 希望它会帮助你。

于 2018-06-19T04:40:27.357 回答