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?