Android Alarm AlarmManager 提供的四种 Alarm 有什么区别以及何时使用什么?

发布于 2024-10-19 12:39:10 字数 343 浏览 3 评论 0原文

我想知道RTC、RTC_WAKEUP、ELAPSED_REALTIME、ELAPSED_REALTIME_WAKEUP之间的区别。
我想编写一个闹钟应用程序,我将在其中设置闹钟并关闭我的应用程序,并期望在设置的时间发出闹钟。
会有多个警报。现在我正在为模拟器编写,但稍后将在设备上进行测试。在模拟器中,一旦我设置闹钟并关闭模拟器并重新启动它,它就会被清除,就像我在 RTC、RTC_WAKEUP 和 ELAPSED_REALTIME 中发现的那样。我很困惑。我应该使用 ELAPSED_REALTIME_WAKEUP 吗?我还没有看到任何使用 ELAPSED_REALTIME_WAKEUP 的教程。 请解释一下。 谢谢。

I want to know the difference between RTC, RTC_WAKEUP, ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP.
I want to write an alarm application where I will set alarm and close my application and expect for alarm for the time set.
There will be multiple alarms. Right now I am writing for emulator but later will test on device. In emulator, once I set the alarm and close the emulator and restart it, then will it be cleared, as I find with RTC, RTC_WAKEUP and ELAPSED_REALTIME. I am confused. Should I used ELAPSED_REALTIME_WAKEUP? I have not seen any tutorial using ELAPSED_REALTIME_WAKEUP.
please explain.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫性小仙女 2024-10-26 12:39:10

ELAPSED_REALTIME

SystemClock.elapsedRealtime() 中的闹钟时间(自启动以来的时间,包括睡眠)。该闹钟不会唤醒设备;如果它在设备睡眠时熄灭,则直到设备下次唤醒时才会传递。

ELAPSED_REALTIME_WAKEUP

SystemClock.elapsedRealtime() 中的闹钟时间(自启动以来的时间,包括睡眠),该时间会在设备关闭时唤醒设备。

System.currentTimeMillis() 中的RTC

闹钟时间(UTC 中的挂钟时间)。该闹钟不会唤醒设备;如果它在设备睡眠时熄灭,则直到设备下次唤醒时才会传递。

RTC_WAKEUP

System.currentTimeMillis() 中的闹钟时间(UTC 中的挂钟时间),当闹钟响起时会唤醒设备。

ELAPSED_REALTIME

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

ELAPSED_REALTIME_WAKEUP

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

RTC

Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

RTC_WAKEUP

Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

多情出卖 2024-10-26 12:39:10

警报类型:

  • ELAPSED_REALTIME – 自设备启动后指定时间长度后触发待处理意图。如果设备处于睡眠状态,它将在设备下次唤醒时触发。
  • ELAPSED_REALTIME_WAKEUP – 自设备启动后指定的时间长度后触发挂起的 Intent。如果设备处于睡眠状态,它会唤醒该设备。
  • RTC – 在指定时间触发待处理的意图。如果设备处于睡眠状态,则直到下次设备唤醒时才会传递。
  • RTC_WAKEUP – 在指定时间触发挂起的意图,如果设备处于睡眠状态则唤醒设备。

Types of Alarms :

  • ELAPSED_REALTIME – Fires the pending intent after the specified length of time since device boot. If the device is asleep, it fires when the device is next awake.
  • ELAPSED_REALTIME_WAKEUP – Fires the pending intent after the specified length of time since device boot. It wakes up the device if it is asleep.
  • RTC – Fires the pending intent at a specified time. If the device is asleep, it will not be delivered until the next time the device wakes up.
  • RTC_WAKEUP – Fires the pending intent at a specified time, waking up the device if asleep.
佞臣 2024-10-26 12:39:10

闹钟有两种通用时钟类型:“经过的实时”和“实时时钟”(RTC)。经过的实时时间使用“系统启动以来的时间”作为参考,实时时钟使用 UTC(挂钟)时间。这意味着经过的实时时间适合根据时间的流逝设置警报(例如,每 30 秒触发一次警报),因为它不受时区/区域设置的影响。实时时钟类型更适合依赖于当前区域设置的警报。

来源:https://developer.android.com/training/scheduling/alarms.html< /a>

There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.

Source: https://developer.android.com/training/scheduling/alarms.html

江湖彼岸 2024-10-26 12:39:10

从该网站您可以得到 4 个常数之间的差异
以下是设置闹钟的示例

Calendar mCalendar = Calendar.getInstance();
        mCalendar.add(Calendar.SECOND, 20);
        Intent intent_Timer = new Intent(TimerEvents.this, AlarmReceiver.class);
        intent_Timer.putExtra("alarm_message", "Drax Rules!!!");
        // In reality, you would want to have a static variable for the request
        // code instead of 192837
        PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
                intent_Timer, PendingIntent.FLAG_UPDATE_CURRENT);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), sender);

希望对您有所帮助

From the site you can get the difference between the 4 constanst
Below is example of the setting alarm

Calendar mCalendar = Calendar.getInstance();
        mCalendar.add(Calendar.SECOND, 20);
        Intent intent_Timer = new Intent(TimerEvents.this, AlarmReceiver.class);
        intent_Timer.putExtra("alarm_message", "Drax Rules!!!");
        // In reality, you would want to have a static variable for the request
        // code instead of 192837
        PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
                intent_Timer, PendingIntent.FLAG_UPDATE_CURRENT);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), sender);

Hope this will be helpful to you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文