问题描述
如何取消 JobIntentService ?除了JobSheduler
API和Context#stopService(...)
方法之外,我在文档中没有其他方法可以做到这一点,我不确定这是否是正确的方法.
How can I cancel a JobIntentService? I see no methods to do so in the docs, apart maybe from the JobSheduler
API and the Context#stopService(...)
method, which I'm unsure if it's the right way.
推荐答案
JobIntentService
仅将IntentService
带到对后台服务更为严格的Android 8+.它在具有旧版Android的设备上像旧版IntentService
一样运行.
JobIntentService
is only bringing IntentService
to Android 8+ that is more strict about background services. It runs like old IntentService
s on devices with older versions of Android.
IntentService
从未被设计为要取消,因此缺少用于取消JobIntentService
的任何API.
IntentService
has never been designed to be cancelled, hence the lack of any API to cancel a JobIntentService
.
因此, JobIntenService
只应用于不需要取消的工作.请注意,JobIntentService
仍可以用于可能失败,因此被中止.
Therefore, JobIntenService
should only be used for work that don't need to be cancelled. Note that JobIntentService
can still be used for work that can fail, and consequently, be aborted.
可以取消的工作应在以下任一情况下执行:
Work that can be cancelled should be executed in either:
- 用于短期工作的
Activity
(或其ViewModel
) - JobService(如果minSdk为21,则使用Firebase Job Dispatcher或Job Scheduler)
- 自定义前台服务
- An
Activity
(or itsViewModel
) for short duration work - A JobService (using Firebase Job Dispatcher or Job Scheduler if minSdk is 21)
- A custom foreground service
这篇关于如何取消JobIntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!