Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Job Scheduler - Schedule Job to execute immediately and exactly once

I am trying to use android job scheduler to schedule a job to execute immediately and exactly once.

        JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

        jobScheduler.cancel(1);

        PersistableBundle bundle = new PersistableBundle();
        bundle.putInt(JobFlags.KEY_PERIODIC_SYNC_JOB, JobFlags.JOB_TYPE_INITIAL_FETCH);

        jobScheduler.schedule(new JobInfo.Builder(1,
                new ComponentName(context, SyncJobLollipop.class))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setExtras(bundle)
                .setMinimumLatency(10)
                .setOverrideDeadline(24 * 3600 * 1000)
                .build());

But it running about 3 4 times. What is wrong here ?

Here is the job class itself:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class SyncJobLollipop extends JobService implements JobFinishedListener {

    @Inject
    SyncJobBackend jobBackend;

    private JobParameters jobParameters;

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        ((MyApplication)getApplication()).getAppComponent().inject(this);
        this.jobParameters = jobParameters;
        PersistableBundle bundle = jobParameters.getExtras();
        int type = bundle.getInt(JobFlags.KEY_PERIODIC_SYNC_JOB);
        jobBackend.onStartJob(type, this);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        jobBackend.onStopJob();
        return false;
    }

    @Override
    public void onJobFinished(boolean success) {
        jobFinished(jobParameters, !success);
    }

}

P.S.: I have checked that I am callig jobFinished with false value each and every time.

like image 739
dumb_terminal Avatar asked Aug 15 '17 11:08

dumb_terminal


1 Answers

To run the job exactly once, you should run your code in onStartJob(...) in a background worker, and remove its callbacks when onStopJob(...) is called.

public class SyncJobLollipop extends JobService {
    final Handler workHandler = new Handler();
    Runnable workRunnable;

    @Override
    public boolean onStartJob(JobParameters params) {
        workRunnable = new Runnable() {
            @Override
            public void run() {
                // do your work here,
                // such as jobBackend.onStartJob(type, this)

                boolean reschedule = false;
                jobFinished(params, reschedule);
            }};
        workHandler.post(workRunnable);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        workHandler.removeCallbacks(workRunnable);
        return false;
    }
}

To run it immediately you should set both "minimum latency" and "override deadline" to 1.

jobScheduler.schedule(new JobInfo.Builder(1,
            new ComponentName(context, SyncJobLollipop.class))
            .setExtras(bundle)
            .setMinimumLatency(1)
            .setOverrideDeadline(1)
            .build());

Please note that the operating system's Doze mode might defer JobScheduler tasks until either a "maintenance period" or the device is awoke in some other way. However, the above code would only be running /outside/ of Doze mode, so the job should be scheduled almost without delay.

like image 164
intrepidis Avatar answered Sep 24 '22 13:09

intrepidis