JobIntentService与IntentService都是可以用来执行后台任务,在Android O之前,使用起来效果没多大差别,但是Android O以后JobIntentService不会立即执行,等手机进入一定状态后才会执行任务,所以不能用来执行及时的后台任务。
When running as a pre-O service, the act of enqueueing work will generally start the service immediately, regardless of whether the device is dozing or in other conditions. When running as a Job, it will be subject to standard JobScheduler policies for a Job with a setOverrideDeadline(long) of 0: the job will not run while the device is dozing, it may get delayed more than a service if the device is under strong memory pressure with lots of demand to run jobs.
the job will not run while the device is dozing 重点是这里的说明。
关于Doze模式的介绍,请参考:
Android中的Doze模式
参考JobIntentService的部分代码:
@RequiresApi