Android 5.1 中的 AlarmManager setExact
AlarmManager setExact in Android 5.1
来自Optimizing for Doze and App Standby:
Doze is particularly likely to affect activities that AlarmManager
alarms and timers manage, because alarms in Android 5.1 (API level 22)
or lower do not fire when the system is in Doze.
To help with scheduling alarms, Android 6.0 (API level 23) introduces
two new AlarmManager methods: setAndAllowWhileIdle() and
setExactAndAllowWhileIdle(). With these methods, you can set alarms
that will fire even if the device is in Doze.
那么,如果我需要在 Android 5.1 中设置准确时间的闹钟,即使是在打瞌睡期间,我该怎么办?不可能吗?
这是我的代码
if (noPreciseTime) {
alarmManager.set(AlarmManager.RTC_WAKEUP, now + interval, pendingIntent)
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent)
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent)
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent)
}
So what do I do if I need to set an alarm for an exact time, even during Doze, in Android 5.1?
设备运行5.1不打瞌睡,所以你不用担心。这个
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent)
将在 5.1 中按预期工作。引入了较新的 setExactAndAllowWhileIdle 以在设备 运行 6.0+ 上使用。您的代码片段没问题。
alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze
这句话有点令人困惑。他们在这里想说的是 警报 使用 5.1 或更低 API 的 设置(如 setExact)当设备处于打瞌睡状态时不会触发 .
在我的实验中,AlarmManagerCompat + setAlarmClock 在休眠模式下可以稳定唤醒。我用的是小米的MIUI
AlarmManagerCompat.setAlarmClock(alarmManager, time, showIntent, pendingIntent)
来自Optimizing for Doze and App Standby:
Doze is particularly likely to affect activities that AlarmManager alarms and timers manage, because alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze.
To help with scheduling alarms, Android 6.0 (API level 23) introduces two new AlarmManager methods: setAndAllowWhileIdle() and setExactAndAllowWhileIdle(). With these methods, you can set alarms that will fire even if the device is in Doze.
那么,如果我需要在 Android 5.1 中设置准确时间的闹钟,即使是在打瞌睡期间,我该怎么办?不可能吗?
这是我的代码
if (noPreciseTime) {
alarmManager.set(AlarmManager.RTC_WAKEUP, now + interval, pendingIntent)
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent)
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent)
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent)
}
So what do I do if I need to set an alarm for an exact time, even during Doze, in Android 5.1?
设备运行5.1不打瞌睡,所以你不用担心。这个
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent)
将在 5.1 中按预期工作。引入了较新的 setExactAndAllowWhileIdle 以在设备 运行 6.0+ 上使用。您的代码片段没问题。
alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze
这句话有点令人困惑。他们在这里想说的是 警报 使用 5.1 或更低 API 的 设置(如 setExact)当设备处于打瞌睡状态时不会触发 .
在我的实验中,AlarmManagerCompat + setAlarmClock 在休眠模式下可以稳定唤醒。我用的是小米的MIUI
AlarmManagerCompat.setAlarmClock(alarmManager, time, showIntent, pendingIntent)