我使用以下代码启动重复任务,我认为logError("OnStop")
将在调用stopScheduleRestore
后启动。
但事实上,logError("OnStop")
没有推出,为什么?
class RestoreService : JobService() {
override fun onStartJob(params: JobParameters): Boolean {
logError("OnStart")
Thread(Runnable { completeJob(params) }).start()
return true
}
override fun onStopJob(params: JobParameters): Boolean {
logError("OnStop")
return false
}
fun completeJob(parameters: JobParameters) {
logError("OK")
jobFinished(parameters, false)
}
}
fun startScheduleRestore(mContext:Context){
val mJobScheduler = mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val jobInfo = JobInfo.Builder(mContext.getInteger(R.integer.JobID), ComponentName(mContext.packageName, RestoreService::class.java.name))
.setPeriodic(interval)
.setPersisted(true)
.build()
mJobScheduler.schedule(jobInfo)
}
fun stopScheduleRestore(mContext:Context){
val mJobScheduler = mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
mJobScheduler.cancel(mContext.getInteger(R.integer.JobID))
}