如何取消JobIntentService?我在文档中没有看到任何方法可以这样做,除了JobSheduler
API和Context#stopService(...)
方法之外,但我不确定这是否是正确的方法。
如何取消JobIntentService?我在文档中没有看到任何方法可以这样做,除了JobSheduler
API和Context#stopService(...)
方法之外,但我不确定这是否是正确的方法。
JobIntentService
将 IntentService
带到了 Android 8+,这是因为 Android 8+ 更加严格控制后台服务。在旧版本的 Android 设备上,它与旧版的 IntentService
运行方式相同。
IntentService
从未被设计成可取消的,因此缺少任何取消 JobIntentService
的 API。
因此,只有不需要取消的任务才应该使用 JobIntentService
。 请注意,JobIntentService
仍然可以用于可能会失败并被终止的工作。
可以取消的工作应该在以下任一位置执行:
Activity
(或其 ViewModel
)中执行JobIntentService
是作为IntentService
实现的。 IntentService
在其onHandleIntent
方法完成前不会停止或通过某些意图操作接受任何其他请求。stopService
()将在任务完成后才被调用,并且当它被调用时会自动停止。这是IntentService
的一个特性。但是一个类似的问题已经在这里关于IntentServices
回答过了,希望能帮到你。
还可以查看Android开发者文档
JobIntentService
是作为IntentService
实现的。虽然您可以使用stopService()
来销毁IntentService
,但这可能不会对用于onHandleIntent()
的后台线程产生影响(我从未尝试过这样做)。 - CommonsWare