setAndAllowWhileIdle 和 setExactAndAllowWhileIdle 的区别

Difference between setAndAllowWhileIdle and setExactAndAllowWhileIdle

AlarmManager.setAndAllowWhileIdle()AlarmManager.setExactAndAllowWhileIdle() 有什么区别?这些都如何影响 Android 6.0 (Marshmallow) API 中引入的打瞌睡模式?

参考AlarmManager的文档:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

确实,您提到的两种方法都指定它们的行为与没有 "AndAllowWhileIdle" 的对应方法(setsetExact)相同。所以其中一个会在准确的时间发出警报,而另一个则不准确。