7

I have a JobService that I would like to start periodically. At the moment, for testing, I am using a trivial one:

public class SenderService extends JobService {

    @Override
    public boolean onStartJob(final JobParameters params) {
        new Thread(new Runnable(){
            public void run(){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                jobFinished(params, false);
            }
        }).start();

        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }
}

I schedule the job like this:

ComponentName serviceName = new ComponentName(this, SenderService.class);

JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setRequiresDeviceIdle(false)
        .setRequiresCharging(false)
        .setPersisted(true)
        .setPeriodic(10000)
        .build();

scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

int result = scheduler.schedule(jobInfo);

if (result == JobScheduler.RESULT_SUCCESS) {
    Log.d(LOGTAG, "job created");
} else {
    Log.d(LOGTAG, "job not created");
}

But the job never starts. If I remove the setPeriodic(), then it starts (but only once of course).

What is going on?

dariosalvi
  • 405
  • 4
  • 13
  • "But the job never starts" -- how long did you wait? You cannot schedule a job with a 10-second period. – CommonsWare May 17 '17 at 15:47
  • 1
    I have waited for minutes. Why 10 seconds is not allowed? The documentation doesn't specify any limitation. I'll try with 2 minutes just in case... – dariosalvi May 17 '17 at 15:54
  • Try 15 minutes: http://stackoverflow.com/a/38774104/115145 – CommonsWare May 17 '17 at 16:01
  • 3
    After Android N, the minimum value for setPeriodic is 15 minutes. If you specify a value less than that, it will be 15 minutes anyway. Let us know if your job runs before the 15 minutes slot (it might run anytime in that slot). – cd1 Aug 09 '17 at 12:04
  • 1
    thanks for your comments. I have noticed that a period of 5 minutes on Android 6 works. Still a long time when you're testing, but not that long. – dariosalvi Aug 09 '17 at 15:24

0 Answers0