Android JobScheduler works too often when using setPeriodic ()

I noticed that my scheduled JobScheduler too often does the job. I have to execute it daily, and it has to be inactive, be on wlan and charge, but when these conditions are met, the work is done like every 10 minutes or even more often.

My code is:

JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(context.getPackageName(), SyncJobService.class.getName())); builder.setPeriodic(TimeUnit.DAYS.toMillis(1)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setRequiresCharging(true) .setRequiresDeviceIdle(true); int code = jobScheduler.schedule(builder.build()); if (code <= 0) { Log.e(TAG, "Could not scheduled job: " + code); return; } Log.e(TAG, "Scheduled job"); 

The task runs a background thread to download data from the Internet, and after the data download is complete, I call

 mService.jobFinished(mParams, true); 

to notify the task scheduler that the task has been completed, and it must transfer it. Why is work done so often, even when the period is set to one day? My Android 6.0.1 device almost never enters dose mode due to the fact that work is done so often.

+7
android synchronization milliseconds android-wifi job-scheduling
source share
1 answer

after the data download is complete, I call mService.jobFinished(mParams, true); to notify the task scheduler that the task has been completed and that it should transfer it

It's your problem. Use false , not true .

false says: "Work done for this job." true says: "We had a problem, please reschedule this work again so that this happens soon." For example, the criteria indicates that you must have an Internet connection, but it may be difficult for you to connect to your server. In this case, return true , in the hope that the problem will clear up a bit.

You can also consider setting deferral rules through setBackoffCriteria() for those cases when you really want to return true .

+12
source share

All Articles