I'm trying to implement a complex, time based, set of rules for scheduling jobs on Android Lollipop
.
I need to run an System API
and a bulk insert
in the DB, so it shouldn't take more than 2-3 seconds. I'll call it JOB1
.
The rules are:
- Run
JOB1
at app startup (easy, just run an AsyncTask at Application onCreate). - Run
JOB1
every approximately 4 hours. - Run
JOB1
ONLY if it wasn't ran in the last 4 hours [optional - not even at app start time] - Run
JOB1
at exactly 23:59 (or 11:59pm), regardless of rule 3.
I don't think that a single instance of JobScheduler
can follow all these rules or at least I still haven't figured it out.
BONUS QUESTION:
JobScheduler jobs = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
JobInfo.Builder b = new JobInfo.Builder(SYNC_JOB_ID,
new ComponentName(this, SyncJobService.class));
b.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE);
b.setPeriodic(getPeriod(6)) //6 hours
.setPersisted(true)
jobs.schedule(b.build());
Calling this at Application
startup will override the previous call or I'll have different instances of JobScheduler
running every 6 hours?