evernote / android-job - 如何安排和定期运行

时间:2018-03-16 19:35:32

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

我在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();
}

1 个答案:

答案 0 :(得分:0)

我面对同样的schenario并使用这个并获得成功。你也可以尝试一下。

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()方法安排相同的作业。你也必须取消上一份工作。

  • 希望它会对你有帮助。