Firebase JobDispatcher:为更频繁执行的每小时安排的作业

时间:2018-04-20 17:03:18

标签: android firebase background kotlin job-scheduling

我的任务应该每小时向服务器发送一次位置。但是Job更频繁地执行(每5-10个人)。 这是我的工作定义:

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()}

Job Service看起来像这样:

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
    }
}

清单定义:

<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>

作业安排在应用程序类的创建上。

执行日志:

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

解决方案

基础on this文章我发现了我的错误。我将 true 作为第二个参数传递给 jobFinished(作业, true ,这意味着我的作业失败,必须根据重试进行重试战略,尽快。我已将其更改为 false ,这解决了我的问题。

0 个答案:

没有答案