我正在使用JobDispacther
在晚上(凌晨1点左右)从我的服务器获取数据。我已经在Jellybean上进行了测试,但是当我在 Nougat 7.1.2(API 25)上进行测试时,它在应用关闭时无效。我正在使用此代码进行测试
class AutoFetch(context: Context){
fun scheduleToFetchAtNight() {
val job = jobDispatcher.newJobBuilder()
.setLifetime(Lifetime.FOREVER)
.setService(AutoFetchService::class.java)
.setTag("fetchAtNight")
.setRecurring(false)
.setReplaceCurrent(true)
.setTrigger(Trigger.executionWindow(60, 60))
.setConstraints(Constraint.ON_ANY_NETWORK).build()
val result = jobDispatcher.schedule(job)
}
}
AutoFetchService
class AutoFetchService : JobService() {
override fun onStartJob(job: JobParameters): Boolean {
toast("Job Started")
AutoFetch(this).scheduleToFetchAtNight()//Reschedule
dataManager.fetchData{
//on Fetched
jobFinished(job, false)
}
return true
}
override fun onStopJob(job: JobParameters): Boolean = true
}
我还在网站上发布了Issue,但仍未收到任何回复,请帮助