1

Okay so I am not able to find any documentation or useful web pages about this. Help me StackOverflow, you're my only hope.

Okay so originally my JobScheduler looks like this:

JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
if(scheduler.getPendingJob(JOB_NUMBER) == null) {
    ComponentName componentName = new ComponentName(this, MyJobService.class);
    JobInfo info = new JobInfo.Builder(JOB_NUMBER, componentName)
            .setRequiresCharging(false)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setPersisted(true)
            .setPeriodic(60 * 60 * 1000L, 5 * 60 *1000)
            .build();
    int resultCode = scheduler.schedule(info);
    if (resultCode == JobScheduler.RESULT_SUCCESS) {
        Log.d(TAG, "Service is not running, Job " + String.valueOf(JOB_NUMBER) + " Scheduled.");
    } else {
        Log.d(TAG, "Service is not running, However job scheduling failed.");
    }
} else{
    Log.d(TAG, "Service is already scheduled.");
}

...Works perfectly in Oreo (v8.0). However in Nougat, v7.0, The job gets 'scheduled' but never run. In another stackoverflow question I asked I found out that i can get it run by replacing setPeriodic() with the following:

.setMinimumLatency(1 * 1000)
.setOverrideDeadline(3 * 1000)

And with that, the service runs. However, this isn't periodically, it will only run once. I cannot find documentation / tutorials / examples that allow me to run a periodic job in Android Nougat. Can anyone help me with this?

There are other stackoverflow questions on this exact same subject:

Job Scheduler not running on Android N

Job Scheduler Not recurring in periodic in Android 7.0 (Nougat)

However neither of them have definitive answers.

Last minute note: Well, it seems that passing the FlexMillis to setPeriodic() seemed to work. I'm going to do more testing. I'm not sure what code I was running when the logcat fired, but I think by passing:

.setPeriodic(15 * 60 * 1000, 5 * 60 *1000)

Into setPeriodic it fired 10 minutes after the job was scheduled. However, unlike Oreo, the job isnt run when its first scheduled. In Oreo as soon as I build the job, the job is run. Again, I can't find this mentioned anywhere in the documentation.

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • did you figure out a solution for this without refactoring and using the new Workmanager? – Ariel Vardi Oct 24 '18 at 20:39
  • @ArielVardi No. And actually the Workmanager worked perfectly. Is there a reason you dont want to use that? – Chud37 Oct 24 '18 at 20:41
  • no reason other than it'd require a larger refactoring, testing, etc. But I guess I go that route if setPeriodic just doesn't do what it's supposed to do on Android N – Ariel Vardi Oct 24 '18 at 20:56
  • 1
    Well if its any consolation it was surprisingly easy to transition. Scheduling the job is very similar and instead of a service you use a worker. – Chud37 Oct 24 '18 at 21:00
  • Thanks, @Chud37 - I did end up switching over and so far so good. It was indeed a trivial code change! – Ariel Vardi Nov 03 '18 at 20:42

1 Answers1

1

You should use https://developer.android.com/topic/libraries/architecture/workmanager/. This is new Android tool and it uses JobScheduler/AlarmManager and so on depending on situation.

TT_W
  • 78
  • 6
  • Thanks. Why I hadn't found out about this before I dont know. But that seems to be the fix for both versions - It works just the same now. The only difference was i had to use a `Worker` instead of a `Service`. And actually scheduling it was a lot simpler too. – Chud37 Sep 21 '18 at 13:12
  • I was glad to help you. – TT_W Sep 21 '18 at 14:46
  • @TT_W can i have sample example. – Vishal Thakkar Mar 13 '19 at 12:37