drmrbrewer
drmrbrewer

Reputation: 13019

Android JobScheduler: job with time constraints

With JobScheduler it is possible to build a JobInfo with certain constraints, like "only when charging" and/or "only when idle" and/or "only when connected to unmetered (e.g. WiFi) network".

Is it possible to specify constraints on when the job is run, like "not between 9.15am and 10.15am"? So you may have this in combination with "only with WiFi", so that if WiFi comes on at 9.30am then all constraints are not yet satisfied and it waits until 10.15am to run.

With a one-off job this seems possible to some extent with builder.setMinimumLatency() and builder.setOverrideDeadline(), but what about for a periodic job established with builder.setPeriodic() (it's not possible to use that in combination with the others)?

Upvotes: 1

Views: 400

Answers (2)

user3294341
user3294341

Reputation: 1

You can do something like this don't know it follows good practices or not but your work will be done ;)

void timeCheck() {
    Date date = Calendar.getInstance().getTime();
    int hour = Integer.parseInt(new SimpleDateFormat("HH").format(date));
    int minute = Integer.parseInt(new SimpleDateFormat("mm").format(date));
    int remainingHours = 10 - hour;
    int remainingMinutes = 15 - minute;
    long timeRemainingInMinutes = remainingHours * 60 + remainingMinutes;
    if (timeRemainingInMinutes >= -60 && timeRemainingInMinutes < 0) {
         yourTask();
    } else {
         // startTimer and on remaining time complete do your task 
    }
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007218

Is it possible to specify constraints on when the job is run, like "not between 9.15am and 10.15am"?

No, sorry.

Upvotes: 0

Related Questions