设置每天特定时间重复闹钟

发布于 2024-12-12 18:22:10 字数 605 浏览 3 评论 0原文

我尝试使用闹钟管理器在每天的特定时间运行闹钟。 我正在使用这段代码,

Intent intent = new Intent(AlarmSettings.this, AlarmService.class);
                        intent.putExtra("i", i);
PendingIntent mAlarmSender = PendingIntent.getService(AlarmSettings.this, Id, intent, 0);

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),Calendar.getInstance().getTimeInMillis()+(24*60*60*1000), mAlarmSender);}

问题出在 如果 cal.getTimeInMillis() 值是过去的,则警报立即运行,我不知道为什么,而当 cal.getTimeInMillis() 值是将来时,它会在当时正确运行。

我需要让它在每天的特定时间运行。

I try to use alarm manager to run alarm at specific time every day.
I am using this code

Intent intent = new Intent(AlarmSettings.this, AlarmService.class);
                        intent.putExtra("i", i);
PendingIntent mAlarmSender = PendingIntent.getService(AlarmSettings.this, Id, intent, 0);

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),Calendar.getInstance().getTimeInMillis()+(24*60*60*1000), mAlarmSender);}

the problem was in
if cal.getTimeInMillis() value is in the past the alarm run immediately, i do not know why, and when cal.getTimeInMillis() value is in the future it runs correctly at its time.

I need to make it run at specific time every day.

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

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

发布评论

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

评论(3

谢绝鈎搭 2024-12-19 18:22:12

这对我帮助很大,我也遇到过使用会议记录的情况,并提出了以下小修改:

/* Create calendar and set desired time before this*/

// Compare the current time milliseconds with the desired calendar time milliseconds
if (java.util.Calendar.getInstance().getTimeInMillis() >= 
            calendar.getTimeInMillis() ) {
    calendar.add(Calendar.DAY_OF_YEAR, 1);
}

This helped me a lot, I had a case where I was using minutes also and came up with the following small amendment:

/* Create calendar and set desired time before this*/

// Compare the current time milliseconds with the desired calendar time milliseconds
if (java.util.Calendar.getInstance().getTimeInMillis() >= 
            calendar.getTimeInMillis() ) {
    calendar.add(Calendar.DAY_OF_YEAR, 1);
}
小伙你站住 2024-12-19 18:22:11
// every day at 9 am
Calendar calendar = Calendar.getInstance();

// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
    calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);

// alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
// AlarmManager.INTERVAL_DAY, pi);
// every day at 9 am
Calendar calendar = Calendar.getInstance();

// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
    calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);

// alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
// AlarmManager.INTERVAL_DAY, pi);
眼眸里的那抹悲凉 2024-12-19 18:22:11

看起来你对

setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

尝试设置正确的triggerAtTime(将来)的调用 - 就像

Calendar.getInstance().getTimeInMillis()+(24*60*60*1000)

第三个参数(间隔)显然应该是你的间隔,就像

24*60*60*1000

It looks like your call to

setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

Try to set proper triggerAtTime (in the future) - like

Calendar.getInstance().getTimeInMillis()+(24*60*60*1000)

The third param (interval) should obviously be your interval, like

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