I am trying to schedule a periodic job to run every 15 mins by using evernote's android-job. The problem is job runs 2-3 times and then it doesn't run every 15 mins. Basically in span of 8 hours it ran almost 10-11 times when the device was sitting idle. Is there something I am doing wrong. The only requirement my job has is Network should be connected which was connected.
Does android-job library somewhat gurantees that job will run? This is my code for the job
public class TestNetworkJob extends Job {
public static final String TAG = "TestNetworkJob";
@NonNull
@Override
protected Result onRunJob(Params params) {
Timber.i("onRunJob");
RetrofitUtils.getInstance().postSlack("TestCall: " + DateTimeUtils.getLocalizedDateTimeString(System
.currentTimeMillis()));
return Result.SUCCESS;
}
public static JobRequest buildJobRequest() {
return new JobRequest.Builder(TAG)
.setUpdateCurrent(true)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPeriodic(TimeUnit.MINUTES.toMillis(15))
.build();
}
}
Basically in span of 8 hours it ran almost 10-11 times when the device was sitting idle
That sounds like standard Doze mode behavior, which you will encounter on Android 6.0+ devices.
I thought that was the whole point of using job-scheduler APIs
Oh, $DEITY
, no. The point behind using JobScheduler
(or wrappers around it) is to be friendly to the battery.
So how can we make sure a job is run every 15 mins?
In general, you don't.
Google would like you to stop polling and switch to Firebase Cloud Messaging, for server-side push messages. You're welcome to experiment with setAndAllowWhileIdle()
/setExactAndAllowWhileIdle()
on AlarmManager
. But, on the whole, the War on Background Processing means that doing periodic background work is difficult at best.