gpt4 book ai didi

java - AlarmManager 与日历对象 Android java

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:33 24 4
gpt4 key购买 nike

你好,我正在尝试制作一个简单的完全工作的闹钟,但我坚持制作适当的闹钟......我不明白的是:

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

// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
  • 我们将当前时间(以米利斯为单位)设置到日历中,因此如果我们得到以米利斯为单位的时间,它就会立即触发,以米利斯为单位设置当前时间,然后分配小时和分钟的目的是什么?

  • 以毫利斯为单位设置时间设置从 1970 年至今的毫秒数?

  • calendar get time in milis 返回 self 们设置的小时以来的 milis 数量?或者 ?我根本不明白这些行,为什么我们要以毫秒为单位设置当前时间,然后设置触发时间?

  • 编辑 - 如果我将闹钟设置为在 10:00 和 10:05 触发,我应该做什么?它立即开火......

最佳答案

we set current time in milis to the calendar so if we get time in milis it will fire right now what is the purpose of setting current time in milis and then assigning the hour and minute ?

(编辑:不完全正确。getInstance() 应该自动将时间设置为当前日期和时间 - 以毫秒为单位设置时间可能与这里无关) - 目的是同时设置当前年/月/日。如果您只设置小时和分钟,则不清楚哪一天会触发。

set time in milis sets the ammount of miliseconds from the year 1970 until now ?

UTC 时区自 1970 年 1 月 1 日以来的当前毫秒数。

calendar get time in milis returns the amount of milis since the hour we set? or ? i dont understand these lines at all, why do we set current time in millis and then set hour to fire?

在这种情况下,getTimeInMillis() 将返回 UTC 时区自 1970 年 1 月 1 日以来到当天 08:30 的毫秒数。

EDIT - if i set alarm to trigger at 10:00 and its 10:05 what should i do ? it fires imediately........

时间可能已经过去了。请记住,时间是 UTC 时间!如果您在美国或亚洲,这可能与您对“现在”的解释有很大不同。

关于java - AlarmManager 与日历对象 Android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39978322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com