Job Scheduler 放弃了 startService() 的性能提升

What performance gains Job Scheduler gives over startService()

Oreo开始,如果应用程序不在前台,服务将无法运行,但我们可以使用JobSchedular进行后台操作。

那么 JobSchedulerstartService() 之间真正的区别,以及为什么 android 支持 JobSchedular 而不是 startService() 的后台操作 oreo.

我仍然可以从后台 ScheduleJobs,这也会影响电池性能。

来自 Android 开发者网站:

The framework will be intelligent about when it executes jobs, and attempt to batch and defer them as much as possible. Typically if you don't specify a deadline on a job, it can be run at any moment depending on the current state of the JobScheduler's internal queue.

While a job is running, the system holds a wakelock on behalf of your app. For this reason, you do not need to take any action to guarantee that the device stays awake for the duration of the job.

当您启动后台服务时,即使应用程序在后台,它也是 运行,因此它会占用资源。使用 JobScheduler 资源仅分配和用于特定作业,并在完成后释放。

From Oreo, a service will not work if the app is not in foreground

是的,会的。它只需要是一个前台服务。

Then what's really is the difference between JobScheduler and startService()

startService() 是即时的。用 JobScheduler 安排的工作不是。 JobScheduler 可以将工作推迟到以后需要完成其他工作时,以尽量减少耗电量的时间(对于 CPU、WiFi、移动数据等) .

此外,startService() 总是会发生。使用 JobScheduler 安排的作业可能不会。您可以为作业设置条件(例如,需要网络连接),只有满足条件时作业才会 运行。

and why android is supporting JobSchedular over startService() for background operations from oreo.

据我所知,主要是功耗。

I can still Schedule tons of Jobs from backround, and it will also effect battery performance.

是的,但是 Google 有更好的 API 可以在将来控制它。如果开发者滥用 JobScheduler,Android 的未来版本可以进一步限制作业速率,扩展作业的 Doze 模式等。