frankelot
frankelot

Reputation: 14419

JobScheduler run job at most once every time the conditions are met

NOTE: THIS IS NOT A DUPLICATE, PLEASE READ THE QUESTION

I want to run a job every time the device is Charging & ON WIFI. This job has to run at most once every time these conditions are met. This means that if I leave the phone charging overnight with wifi ON the job should not repeat itself. Only when I unplug and replug the job is allowed to execute again. Same goes for when I turn wifi off and on.

    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setRequiresCharging(true)
            //.setPeriodic(TimeUnit.SECONDS.toMillis(10))
            .setPersisted(true)  

Job scheduler provides methods like setPeriodic but that will run my job every X amount of time. Not really what I want. The job is not critical, I don't need it to be executed right away after the conditions are met, and I'm also OK with it not executing at all sometimes (meaning it's ok for it not to be run when conditions are met for a short period of time)

Is it possible to achieve this using job scheduler? The documentation on this is pretty scare.

Upvotes: 1

Views: 1210

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199880

If you're not using setPeriodic, then your job would only run once when your other constraints are set. However, your requirements mean you need to schedule a new job when you leave those conditions - JobScheduler does not offer that API, nor does Android offer any API that does that that also works with Android 8.0's Background Execution Limits (with the exception of continuously running a foreground service).

Upvotes: 3

Related Questions