onCreate
onCreate

Reputation: 775

Android JobScheduler always works for 1 minute

I'm trying to implement background refreshing service with new JobScheduler(compat by tatarka). Here is my Service

@Override
public boolean onStartJob(JobParameters params) {
    Timber.i("on start job: " + params.getJobId());
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    Timber.i("on stop job: " + params.getJobId());
    return true;
}

Here is my JobInfo

public void scheduleJob(View v) {
    JobInfo job = new JobInfo.Builder(kJobId++ /*jobid*/, new ComponentName(getActivity(), RefreshJobService.class))
            .setPeriodic(2000)
            .build();
    mTestService.scheduleJob(job);
}

In log i see that my job always works for 1 minute 12-31 12:38:03.884 10059-10059/@/RefreshJobService﹕ on stop job: 0 12-31 12:39:03.891 10059-10059/@/RefreshJobService﹕ on start job: 0 12-31 12:40:03.911 10059-10059/@/RefreshJobService﹕ on stop job: 0 12-31 12:42:08.841 10059-10059/@/RefreshJobService﹕ on start job: 0 12-31 12:43:08.858 10059-10059/@/RefreshJobService﹕ on stop job: 0

So why? I'm set periodic to 2000ms, any value do not influence to 1 minute interval for job. Why?

Upvotes: 11

Views: 8313

Answers (5)

Imran Haidry
Imran Haidry

Reputation: 108

From API 26 (Oreo), setting a smaller period than 15 minutes for periodic jobs will execute at least after 15 minutes.

Upvotes: 7

Tom
Tom

Reputation: 7804

onStopJob is called when the system wants to cancel your job. The reason it is cancelling your job is because it thinks it's still running. You need to call jobFinished(params, bool) at some point in your job to tell the scheduler that it is complete, otherwise it will time out and call onStopJob to cancel it. In addition, returning 'true' from onStartJob tells the system that you are not done processing. You should return 'false' unless you are still doing extra work, eg. passing the work off to a separate thread.

@Override
public boolean onStartJob(JobParameters params) {
    Timber.i("on start job: " + params.getJobId());
    jobFinished(params, false);
    return false;
}

Upvotes: 8

greywolf82
greywolf82

Reputation: 22173

Until Android 5.1.1 there was a timeout of 60 seconds for a single job. Starting from Android 6, the timeout is now of 10 minutes.

Upvotes: 8

gaurav jain
gaurav jain

Reputation: 3234

43matthew is correct, but I'd like to add something more to this. The 2000 ms that you're talking about is the periodic interval after which the job starts again i.e. execute this job every 2000 ms, not the interval for which the job will execute.

Upvotes: 0

43matthew
43matthew

Reputation: 982

It sounds like you are asking why is it that you get onStopJob exactly one minute after your job is started. This 1 minute timeout is defined in code here

Upvotes: 3

Related Questions