Chud37
Chud37

Reputation: 5027

Periodic Job in Android N (Nougat, 7.0)

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.

Upvotes: 1

Views: 451

Answers (1)

TT_W
TT_W

Reputation: 78

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.

Upvotes: 1

Related Questions