17

The JobScheduler calls onStartJob() multiple times, although the job finished. Everything works fine, if I schedule one single job and wait until it has finished. However, if I schedule two or more jobs with different IDs at the same time, then onStartJob() is called again after invoking jobFinished().

For example I schedule job 1 and job 2 with exactly the same parameters except the ID, then the order is:

  1. onStartJob() for job 1 and job 2
  2. Both jobs finish, so jobFinished() is invoked for both of them
  3. After that onStartJob() is called again for both jobs with the same ID

My job is very basic and not complicated.

public class MyJobService extends JobService {

    @Override
    public boolean onStartJob(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // do something

                } finally {
                    // do not reschedule
                    jobFinished(params, false);
                }
            }
        }).start();

        // yes, job running in the background
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        // mark my background task as stopped

        // do not reschedule
        return false;
    }
}

I schedule the jobs like this

JobInfo jobInfo = createBaseBuilder(request)
        .setMinimumLatency(2_000L)
        .setOverrideDeadline(4_000L)
        .setRequiresCharging(false)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .build();

int scheduleResult = mJobScheduler.schedule(jobInfo);
// is always success

I don't know what's wrong.

vRallev
  • 4,982
  • 3
  • 31
  • 34
  • I'm having the same problem, @vRallev. The only way I could avoid the job running twice is to return `false` from `onStartJob()` (even though I have set an `AsyncTask` running at that time). Whilst this seems to work for me, I suspect it will cause problems in future, where the system thinks the job has finished and kill the process prematurely. Did you ever find a solution to this? – drmrbrewer Sep 09 '16 at 17:36
  • I remember if a job was already started. That works for me, see https://github.com/evernote/android-job/blob/master/library/src/main/java/com/evernote/android/job/v21/PlatformJobService.java And: https://github.com/evernote/android-job/blob/master/library/src/main/java/com/evernote/android/job/JobProxy.java#L110 – vRallev Sep 23 '16 at 09:37

3 Answers3

4

I guess it's caused by the pending Job, so I call mJobScheduler.cancelAll() after the service started, problem resolved.

Xande
  • 41
  • 2
  • 2
    I don't understand. You schedule the job (JobService), then straight away call cancellAll()? Shouldn't it be up to the JobService to stop itself when it is done (and not have the system start it up again, as per the OP)? – drmrbrewer Sep 09 '16 at 18:58
3

I think this relates to the Android bug reported here, which has apparently been fixed for Android N but will be present in earlier versions.

The OP is using a setOverrideDeadline(). My understanding of the issue reported in the linked post above is that if the job is running when the override deadline fires, it causes the job to be scheduled to run again.

So the advice is to ensure that the override fires either before the job is scheduled (not sure how that is achieved) or after it has finished. Neither seems particularly satisfactory, but at least it seems to have been fixed in Android N.

Community
  • 1
  • 1
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
1

this is the problem in android lollypop and Marshmallow. It is fixed in Nougat as explained by Matthew Williams here

Community
  • 1
  • 1
Imran Khan Saifi
  • 561
  • 1
  • 4
  • 16