android.app.job.JobScheduler 代替 WakefulBroadcastReceiver 用法

android.app.job.JobScheduler instead of WakefulBroadcastReceiver usage

正如 here 所述,通常的 class WakefulBroadcastReceiver 已弃用。

因此,现在无法像以前的 SDK 版本那样创建计划任务。 Google 说,我必须使用

https://developer.android.com/reference/android/app/job/JobScheduler.html

但是如何呢?

那么,现在最好用的后台定时任务方案是什么?

AlarmManager 不再有用了吗?

我的应用程序每天唤醒几次并使用 AlarmManager 更新通知。我不太确定我必须改变什么。我必须实施什么?旧的 API 和新的?或者只有最新的解决方案?

now this is not possible to create a scheduled tasks like for previous SDK releases

WakefulBroadcastReceiver 从来都不是唯一的选择。例如,我的 WakefulIntentService 可以追溯到 2009 年。添加到支持库中的 the JobIntentService 取代了这两者。

But how?

嗯,JobScheduler 包含在 the JavaDocs and elsewhere in the documentation 中,还有关于 Android 应用程序开发的书籍和课程。

what's the most useful solution for background scheduled tasks nowadays?

这完全取决于你想如何定义"useful",我不知道你的定义。

一般来说:

  • 尽可能使用JobScheduler

  • 如果您正在编写闹钟应用程序,请在 AlarmManager 上使用 setAlarmClock()

  • AlarmManager 上使用其他 set...() 方法以向后兼容 pre-Android 5.0 设备,可能是通过包装库(例如,Evernote 的 android-job)

  • 在可行的情况下使用推送消息(例如 FCM),如果您能够依赖该推送消息解决方案

Is AlarmManager not longer useful?

对于 "useful" 的某些定义,任何 类型的周期性后台工作是 "no longer useful"。 Android 6.0 中引入的打瞌睡模式和应用程序待机,从用户的角度来看基本上使周期性的后台工作变得不可靠。

What I have to implement? The old API and a new one? Or only up-to-date solution?

抽象地回答这个问题是不可能的。 Android 8.0 或支持库中没有任何内容可以阻止您像以前一样使用 AlarmManager