简体   繁体   English

当闹钟时间与实时时间相距1小时的倍数时,会触发PendingIntent

[英]PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android

I am creating an Alarm clock app. 我正在创建一个闹钟应用程序。 I can set PendingIntent 's, cancel them, and receive them using my BroadcastReceiver when the time that I set using AlarmManager is reached. 当达到使用AlarmManager设置的时间时,可以设置PendingIntent ,取消它们并使用我的BroadcastReceiver接收它们。 I discovered a recent problem however. 我发现了最近的问题。

Previously, I have been able to set an alarm for any time in the future, and the BroadcastReceiver wouldn't "receive" the PendingIntent until that time was reached. 以前,我可以在将来的任何时候设置警报,并且BroadcastReceiver直到到达该时间才可以“接收” PendingIntent。 I guess I never covered the scenario where the alarm to be set is exactly 1 or more (integers only) hours away. 我想我从未涉及过要设置的警报正好在1小时或更长时间(仅限整数)的情况。 For example, the current time is 11:54, and I set an alarm for 12:54, or 1:54, 2:54, etc. When I do this, the BroadcastReceiver receives the PendingIntent and performs the actions that I told it to do. 例如,当前时间是11:54,我将警报设置为12:54或1:54、2:54等。当我这样做时,BroadcastReceiver会收到PendingIntent并执行我告诉它的操作去做。

Why does this happen? 为什么会这样? When I change the minute to something different, it doesn't happen, only if the minute is the same the app behaves as if I set the alarm for the current time. 当我将分钟更改为其他内容时,只有当分钟与应用程序的行为相同,就像我将闹钟设置为当前时间一样时,它才不会发生。

This is how I set the alarms: 这是我设置警报的方式:

public void scheduleAlarm(Context aContext) {
    AlarmManager am = (AlarmManager) aContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(aContext, MyBroadcastReceiver.class);

    String id = this.getId().replaceAll("[^0-9]+", "");      // this.getId returns a string such as "alarm1". We only need the "1".

    PendingIntent alarmIntent = PendingIntent.getBroadcast(aContext, Integer.parseInt(id), intent, 0);

    // "this" in this context is the Alarm object. So you can get the hour and minute from the timepicker used to set the alarm
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, this.getHour());
    calendar.set(Calendar.MINUTE, this.getMinute());

    long calendarTime = calendar.getTimeInMillis();
    am.setExact(AlarmManager.RTC_WAKEUP, calendarTime, alarmIntent);
}

I checked the time that the alarm is set for, and confirmed that they were not the present time so they should not go off. 我检查了设置闹钟的时间,并确认它们不是当前时间,因此不应关闭。 After trying to set multiple alarms and seeing that the problem was still happening, I uninstalled the app from my phone, and gave it a fresh install. 尝试设置多个警报并发现问题仍然存在后,我从手机上卸载了该应用程序,并进行了全新安装。 Everything worked fine afterwards and is still functioning correctly. 之后,一切正常,并且仍然可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果时区不同于格林尼治标准时间(UTC),则AlarmManager在错误的时间触发警报 - AlarmManager fires alarm in the wrong time if timezone is different than GMT(UTC) 服务启动后而非设定时间触发警报 - Alarm fires after service start instead of set time 将当前时间(dt)从秒转换为实时格式(小时和分钟) - Convert current time(dt) from seconds to real time format(hour and minute) 在准确的时间内运行PendingIntent - Run PendingIntent in exact time Android AlarmManager:如何找出触发PendingIntent的剩余时间 - Android AlarmManager: how to find out the time remaining to trigger the PendingIntent 如何从本地android闹钟应用程序获取时间。 是否有机会从AlarmClock功能获取时间? - How to get the time from native android alarm clock application. Is there a chance to get the time from AlarmClock function? Android Alarm Manager不适用于特定日期和时间 - Android Alarm Manager not working with specific date and time 基于共享首选项中存储的时间的Android警报 - Android Alarm Based On a Time Stored in Shared Preferences 为Android设置基于数据库值/时间的警报? - Set an alarm based of a Database value/time for Android? Android在Alarm Manager中设置特定的时间和日期 - Android set specific time and date in Alarm Manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM