为什么JobScheduler无法正常工作

时间:2019-06-20 12:37:10

标签: android service android-jobscheduler jobservice

我正在使用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分钟不工作一次。我也想在特定时间开始工作。例如,每天八点钟。如何使用JobSceduler实现这一目标。

1 个答案:

答案 0 :(得分:0)

JobScheduler不能保证每15分钟运行一次您的工作,但可以保证每隔15分钟运行一次。参见this answer