java - AlarmManager 与日历对象 Android java

标签 java android

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

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/

相关文章:

android - res文件夹和R.java的问题

Android GridLayout 只显示最后一个 child

java - 执行 RDP 时 Sukuli 按钮识别

java - 无法使用 JBOSS Tools 3.3.o M1 在新的 Eclipse Indigo M6a Java EE 中创建 JSF 项目

java - 如何在 nifi 中拆分街道名称和街道号码中的地址?

java - 查询自定义对象时出现 ParseException : no results found for query. 代码 101

java - 由 : java. lang.ClassCastException : com. example.mayank.sunshine.SettingsActivity 引起,无法转换为 android.app.Activity

java - 为通知操作设置不同的颜色

android - GCM : messages by IntentService. 如果应用程序不可见,则不会收到 sendBroadcast

java - 在 JAVA 中创建 SOAP POST