Android AlarmManager 的 setRepeating 和 setInexactRepeating 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21232984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:11:15  来源:igfitidea点击:

Difference between setRepeating and setInexactRepeating of AlarmManager

androidalarmmanagerandroid-alarms

提问by User3

What are the parameters of the following:

下面的参数是什么:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);

And of the following:

以及以下内容:

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

What is the difference and also how are the two different in terms of functionality?

两者有什么区别以及两者在功能方面有何不同?

回答by PaF

Both examples schedule a repeating alarm that will send the given alarmIntent. On both cases, the first time it is sent will be immediate (calendar.getTimeInMillis()returns the current time). On both cases, the device will be woken up when the alarm needs to be sent (as evident by AlarmManager.RTC_WAKEUP).

这两个示例都安排了一个重复的警报,该警报将发送给定的alarmIntent. 在这两种情况下,第一次发送将是立即的(calendar.getTimeInMillis()返回当前时间)。在这两种情况下,设备将在需要发送警报时被唤醒(如 所示AlarmManager.RTC_WAKEUP)。

There are two differences between these calls. The simpler one is that the intent will be sent every fifteen minutes on the first call, and every day on the second call (as you can see in the third parameter). The more complicated difference is the function call itself: setRepeatingwill schedule the first alarm for exactlyevery fifteen minutes; setInexactRepeatingwill schedule the second alarm for approximatelyevery 24 hours, meaning it might deviate from that interval - with the advantage of consuming less power.

这些调用之间有两个区别。更简单的是,意图将在第一次调用时每 15 分钟发送一次,在第二次调用时每天发送一次(如您在第三个参数中所见)。更复杂的区别是函数调用本身:setRepeating将安排第一个报警恰好每15分钟; setInexactRepeating大约每 24 小时安排第二次警报,这意味着它可能会偏离该间隔 - 具有消耗更少电量的优势。

Do notice that this has changed in API 19, where these two calls are synonymous. See this guide, and this API documentation.

请注意,这在 API 19 中发生了变化,这两个调用是同义词。请参阅本指南此 API 文档

回答by Mehul Joisar

Decide how precise your alarm needs to be

确定您的闹钟需要有多精确

Choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be.

选择警报类型通常是创建警报的第一步。另一个区别是您需要警报的精确度。

For most apps, setInexactRepeating()is the right choice. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. This reduces the drain on the battery.

对于大多数应用程序,setInexactRepeating()是正确的选择。当您使用此方法时,Android 会同步多个不精确的重复警报并同时触发它们。这减少了电池的消耗。

For the rare app that has rigid time requirements as example, the alarm needs to fire preciselyat 4:00 p.m. everyday then use setRepeating().

例如,对于具有严格时间要求的罕见应用程序,闹钟需要在每天下午 4:00精确触发,然后使用setRepeating().

Reference: Decide how precise your alarm needs to be

参考:确定您的闹钟需要有多精确

回答by fingertricks

To augment previous answers, there are a number of other best practices to consider when using repeating alarms, particularly inexact alarms requested using setInexactRepeating().

为了补充以前的答案,在使用重复警报时,还有许多其他最佳实践需要考虑,特别是使用setInexactRepeating().

Alarm Type

报警类型

  • Non-WAKEUP alarms are better than WAKEUP alarms from a power management perspective. Using the former your alarm may fire late, but it will still fire either when the device is woken by the user, or when another wakeup alarm fires. Using WAKEUP alarms will wake the device out of sleep, consuming additional battery and potentially causing other inexact alarms to fire that have been delayed that could otherwise have been delayed longer (reducing the batching power-saving benefits that inexact alarms provide).
  • Prefer alarms using the ELAPSED timebase rather than the RTC timebase. The former are more likely to have a more random distribution across devices than RTC alarms, which reduces the risk of network congestion and on the server if the alarm is triggering some sort of poll. Phones running Gingerbread (or older) suffer from a bug whereby RTC inexact alarms have a tendency to align closely to the real-time clock, e.g. approximately 30s past each quarter of an hour. ELAPSED alarms don't suffer from this bug on these earlier platform versions. Even if your alarm doesn't trigger any network activity, remember that if it is a wakeup alarm it may trigger other alarm non-wakeup intents that may hit the network.
  • 从电源管理的角度来看,非 WAKEUP 警报比 WAKEUP 警报更好。使用前者,您的闹钟可能会延迟触发,但当用户唤醒设备或另一个唤醒闹钟触发时,它仍会触发。使用 WAKEUP 警报会将设备从睡眠中唤醒,消耗额外的电池,并可能导致其他延迟的不准确警报触发,否则可能会延迟更长时间(减少不准确警报提供的批处理省电优势)。
  • 更喜欢使用 ELAPSED 时基而不是 RTC 时基的警报。前者比 RTC 警报更有可能在设备之间具有更随机的分布,如果警报触发某种轮询,这会降低网络拥塞和服务器上的风险。运行 Gingerbread(或更早版本)的电话存在一个错误,即 RTC 不精确警报倾向于与实时时钟紧密对齐,例如每一刻钟后大约 30 秒。ELAPSED 警报在这些早期平台版本上不会受到此错误的影响。即使您的警报没有触发任何网络活动,请记住,如果它是唤醒警报,它可能会触发其他可能影响网络的警报非唤醒意图。

Timebase

时基

  • Be careful to specify the requested start time in the correct time domain for the alarm type. Failure to do this can result in alarms being set in the past (they fire right away) if setting an RTC alarm with an ELAPSED timebase or far in the future if setting an ELAPSED alarm using the RTC timebase. You can check what alarms an app has scheduled using dumpsys alarmvia the adb shell.
  • 小心在正确的时域中为警报类型指定请求的开始时间。如果使用 ELAPSED 时基设置 RTC 警报,或者如果使用 RTC 时基设置 ELAPSED 警报,则不这样做可能会导致过去设置警报(它们会立即触发)。您可以dumpsys alarm通过adb shell.

Interval

间隔

  • Specifying an inexact alarm interval of anything other than the interval constants defined in the AlarmManager API is redundant on SDK <19: they will be scheduled as exact not inexact alarms, losing all the power-saving benefits that inexact alarms provide.
  • 在 SDK <19 上,指定除 AlarmManager API 中定义的间隔常量以外的任何内容的不精确警报间隔是多余的:它们将被安排为精确而不是不精确的警报,从而失去不精确警报提供的所有节能优势。

Edit: here's further explanation of the bug relating to gingerbread and honeycomb 3.0 devices: https://code.google.com/p/android/issues/detail?id=31550

编辑:这里是与姜饼和蜂窝 3.0 设备相关的错误的进一步解释:https: //code.google.com/p/android/issues/detail?id =31550

回答by Jesus Dimrix

setRepeating is more accurateand setInexactRepeating is for saving battery but no accurate, setInexactRepeating is good for maintenance in background for example and setRepeating is necessary for example for alarm clock .

setRepeating 更准确setInexactRepeating 用于节省电池但不准确, setInexactRepeating 有利于后台维护,例如, setRepeating 是必要的,例如闹钟。

回答by Karamat Subhani

Use setInexactRepeating() is used when app is not seriously used to required for example wake up early in the morning . if Alarm wake up approximately that time there is no any life dangeous. like medicine pills app where highly critical patient use that app to reminde nurse or doctor staff the is compulsory to use setRepeating(). When you use setInexactRepeating() then Android synchronizes repeating alarms from multiple apps and fires them at the same time(once). This reduces the total number of times the system must wake the device. thus reducing drain on the battery. repeating alarms are inexact. Note that while setInexactRepeating() is an improvement over setRepeating() also it can still overwhelm a server if every instance of an app hits the server around the same time. Therefore, for network requests, add some randomness to your alarms.

使用 setInexactRepeating() 用于应用程序不是很习惯需要时,例如早上起床。如果警报大约在那个时间醒来,则没有任何生命危险。像药丸应用程序,其中高度危急的患者使用该应用程序来提醒护士或医生工作人员必须使用 setRepeating()。当您使用 setInexactRepeating() 时,Android 会同步来自多个应用程序的重复警报并同时(一次)触发它们。这减少了系统必须唤醒设备的总次数。从而减少电池的消耗。重复的警报是不准确的。请注意,虽然 setInexactRepeating() 是对 setRepeating() 的改进,但如果应用程序的每个实例大约在同一时间访问服务器,它仍然可以压倒服务器。因此,对于网络请求,请为警报添加一些随机性。