为什么不启动onStopJob?

时间:2018-05-10 06:45:52

标签: android

我使用以下代码启动重复任务,我认为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))

}

0 个答案:

没有答案