为什么作业计划程序无法按预期工作



我正在使用JobScheduler每15分钟运行一次作业。但它没有按预期工作。在这里,我附加了我的代码。

public class WorkScheduler extends JobService {
    private static final String TAG = "Jobschedulerexample";
    private boolean jobCanceled = false;
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Log.d(TAG,"Started at ==> "+ sdf.format(new Date()));
        Log.d(TAG,"Job Started");
        doBackgroundWork(jobParameters);
        return true;
    }
    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.d(TAG, "Job cancelled before completion");
        jobCanceled = true;
        return true;
    }
    private void doBackgroundWork(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    Log.d(TAG, "run: " + i);
                    if (jobCanceled) {
                        return;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Log.d(TAG, "Job finished");
                Log.d(TAG,"Ended at ==> "+ sdf.format(new Date()));
                jobFinished(params, true);
            }
        }).start();
    }
}

作业服务的实用工具类

public class WorkSchedulerUtility {
    private static final int JOB_ID = 123;
    private static WorkSchedulerUtility mInstance;
    private final int REFRESH_INTERVAL = 15 * 60 * 1000;
    private WorkSchedulerUtility(){
    }
    public static WorkSchedulerUtility getInstance(){
        if(mInstance == null){
            mInstance = new WorkSchedulerUtility();
        }
        return mInstance;
    }
    private static final String TAG = "Jobschedulerexample";
    public void scheduleJob(Context context) {
        ComponentName componentName = new ComponentName(context, WorkScheduler.class);
       /* JobInfo info = new JobInfo.Builder(JOB_ID, componentName)
                .setPersisted(true)
                .setPeriodic(REFRESH_INTERVAL)
                .build();*/
        JobInfo info = new JobInfo.Builder(JOB_ID, componentName)
                .setPersisted(true)
                .setBackoffCriteria(REFRESH_INTERVAL, JobInfo.BACKOFF_POLICY_LINEAR)
                .setMinimumLatency(1000 * 6)
                .build();
        JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d(TAG, "Job scheduled");
        } else {
            Log.d(TAG, "Job scheduling failed");
        }
    }
    public void cancelJob(Context context) {
        JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
        scheduler.cancel(123);
        Log.d(TAG, "Job cancelled");
    }
}

我计划每 15 分钟运行一次作业。但它不是每 15 分钟开始一次工作。我将附加输出。

开始于 ==> 15:10:49 结束于 ==>

15:10:59 开始于 ==> 15:25:59 结束于 ==> 15:26:09 开始于 ==> 15:56:10 结束于 ==> 15:56:20 开始于 ==> 16:41:44 结束于 ==> 16:41:54

我不知道为什么它每 15 分钟不起作用。我也想在特定时间开始工作。例如每天8点钟。如何使用JobSceduler实现这一目标。

JobScheduler 不保证每 15 分钟运行一次作业,而是在它认为合适的时候运行一次。看到这个答案。

相关内容

  • 没有找到相关文章

最新更新