I have a task that should send location every hour to the server. However Job is executed more frequently (every 5-10 muntes). Here is my job definition:
fun createJob(firebaseJobDispatcher: FirebaseJobDispatcher): Job? {
logd(TAG, "create job ")
val windowStart = TimeUnit.MINUTES.toSeconds(50).toInt()
return firebaseJobDispatcher.newJobBuilder()
.setLifetime(Lifetime.FOREVER)
.setService(LocationJobService::class.java)
.setTag("LocationJobService")
.setReplaceCurrent(false)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(windowStart, windowStart +TimeUnit.MINUTES.toSeconds(10).toInt()))
.setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
.build()}
The Job service looks like this:
class LocationJobService : JobService() {
companion object {
private const val TAG = "LocationJobService"
}
override fun onStopJob(job: JobParameters): Boolean = true
override fun onStartJob(job: JobParameters): Boolean {
logd(TAG, "Job started")
Thread({
LocationManager.updateAndSendLocationIfPossible()
jobFinished(job, true)
}).start()
return true
}
}
Manifest definition:
<service
android:exported="false"
android:name=".services.LocationJobService"
android:permission="android.permission.BIND_JOB_SERVICE">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
Job scheduled onCreate of the application class.
The log of execution:
04-20 18:24:10.256 11796-11796/ D/LocationJobService: Job started
04-20 18:28:25.264 11796-11796/ D/LocationJobService: Job started
04-20 18:31:26.873 11796-11796/ D/LocationJobService: Job started
04-20 18:34:20.639 11796-11796/ D/LocationJobService: Job started
04-20 18:43:12.631 11796-11796/ D/LocationJobService: Job started
Solution
Base on this article I found my mistake. I passed true as the second parameter to jobFinished(job,true) and this means that my Job failed and must be retried based on a retry strategy, as soon as possible. I have changed it to false and this fixed my issue.