因此,我目前使用JobScheduler根据各种条件来调度作业,我想我会想要使用JobIntentService来执行它们。但是,我看到JobIntentService也有一个enqueueWork()方法。这是JobScheduler的替代品吗?它是可选的吗?这样我就可以忽略它,只使用JobScheduler来调度任务,让JobIntentService只关心执行?
谢谢。
发布于 2017-11-04 19:50:37
为什么要使用JobScheduler来运行JobIntentService?根据官方文档,JobIntentService will be subject to standard JobScheduler policies for a Job with a setOverrideDeadline(long) of 0和您不能对其应用其他JobScheduler's选项。您必须使用它自己的enqueueWork方法来运行它,
enqueueWork(applicationContext, Intent(applicationContext, MyJobIntentService::class.java))您的服务可以这样开发:
class MyJobIntentService : JobIntentService() {
val TAG = "TAG_MyJobIntentService"
override fun onHandleWork(intent: Intent) {
// do your work here
Log.i(TAG, "Executing work: " + intent)
}
companion object {
internal val JOB_ID = 1000
internal fun enqueueWork(context: Context, work: Intent) {
JobIntentService.enqueueWork(context, MyJobIntentService::class.java, JOB_ID, work)
}
}
}别忘了把它放到你的载货单上
<service
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"
android:name=".MyJobIntentService">
</service>https://stackoverflow.com/questions/46338439
复制相似问题