Akhil Soman
Akhil Soman

Reputation: 31

Periodic job in Android Job Scheduler not working as expected

I have a requirement to sync data from server to my local device in specified time interval, for that I am using Android JobScheduler. But it is not working as expected. For example I scheduled periodic job in every 30 minute, at the first time it worked properly. But next times the job is executed before 15 minutes, I debugged the interval time but it is always 30 minutes.

Only 1st time it working correctly.

This is the pending job information log:

Service Name ->ComponentInfo{my pakage name.services.SyncServiceFare}Time Interval->1800000}

Service Name ->ComponentInfo{my pakage name.services.SyncService}Time Interval->1800000}

This is my code:

private fun schedulePeriodicJob(JOB_ID: Int, fetchInterval: Long) {
        jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
        var serviceName : ComponentName? = null
        try {
            if (jobScheduler == null) {
                return
            }
            jobScheduler?.cancel(JOB_ID)

            serviceName = if(JOB_ID == 1){
                ComponentName(this, SyncService::class.java)
            }else{
                ComponentName(this, SyncServiceFare::class.java)
            }
            val builder = JobInfo.Builder(JOB_ID, serviceName)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setBackoffCriteria(60000, JobInfo.BACKOFF_POLICY_EXPONENTIAL)
                .setPersisted(true)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                builder.setPeriodic(
                    TimeUnit.MINUTES.toMillis(fetchInterval),
                    JobInfo.getMinFlexMillis()
                )
            } else {
                builder.setPeriodic(TimeUnit.MINUTES.toMillis(fetchInterval))
            }
            val extras = PersistableBundle()
            val scheduleTime = System.currentTimeMillis()
            extras.putLong("SCHEDULE_TIME", scheduleTime)
            builder.setExtras(extras)
            jobScheduler?.schedule(builder.build())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

How can I solve this issue?

Upvotes: 2

Views: 1225

Answers (1)

Daniel
Daniel

Reputation: 869

The period you are setting for the JobScheduler only means that the process will be executed inside a window of time as large as that period. You do not know when it will run: the system will calculate the best time for the execution the task (probably grouping it together with other scheduled tasks) in order to reduce battery usage and keep the phone in idle state as long as possible.

The next scheduled execution will be inside another window o time that starts as soon as the task has finished. That is why you are seeing inconsistent times and this is by design.

If you need to execute jobs at exact times then you should look at AlarmManager's setExact method, although this is strongly discouraged since it will negatively impact the battery consumption of the device.

Upvotes: 2

Related Questions