首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JobScheduler和JobIntentService

JobScheduler和JobIntentService
EN

Stack Overflow用户
提问于 2017-09-21 15:56:42
回答 1查看 1.4K关注 0票数 2

因此,我目前使用JobScheduler根据各种条件来调度作业,我想我会想要使用JobIntentService来执行它们。但是,我看到JobIntentService也有一个enqueueWork()方法。这是JobScheduler的替代品吗?它是可选的吗?这样我就可以忽略它,只使用JobScheduler来调度任务,让JobIntentService只关心执行?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 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方法来运行它,

代码语言:javascript
复制
enqueueWork(applicationContext, Intent(applicationContext, MyJobIntentService::class.java))

您的服务可以这样开发:

代码语言:javascript
复制
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)
        }
    }
}

别忘了把它放到你的载货单上

代码语言:javascript
复制
<service
     android:permission="android.permission.BIND_JOB_SERVICE"
     android:exported="false"
     android:name=".MyJobIntentService">
</service>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46338439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档