0

我使用 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 实现这一目标。

4

1 回答 1

0

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

于 2019-06-20T13:16:53.247 回答