gpt4 book ai didi

java - 当闹钟时间与实时时间相差 1 小时的倍数时,PendingIntent 会触发 - Android

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

我正在创建一个闹钟应用程序。我可以设置 PendingIntent,取消它们,并在到达使用 AlarmManager 设置的时间时使用我的 BroadcastReceiver 接收它们。然而我最近发现了一个问题。

以前,我可以为 future 的任何时间设置闹钟,并且在到达该时间之前,BroadcastReceiver 不会“接收”PendingIntent。我想我从来没有讨论过要设置的闹钟距离正好 1 小时或更多(仅限整数)小时的情况。例如,当前时间是 11:54,我将闹钟设置为 12:54,或者 1:54、2:54 等。当我这样做时,BroadcastReceiver 会收到 PendingIntent 并执行我告诉它执行的操作。

为什么会发生这种情况?当我将分钟更改为不同的值时,这种情况不会发生,只有当分钟相同时,应用程序的行为就像我为当前时间设置了闹钟一样。

这就是我设置闹钟的方式:

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);
}

最佳答案

我检查了闹钟设置的时间,并确认它们不是当前时间,因此它们不应响起。在尝试设置多个闹钟并发现问题仍然发生后,我从手机上卸载了该应用程序,并重新安装了它。之后一切正常,并且仍然正常运行。

关于java - 当闹钟时间与实时时间相差 1 小时的倍数时,PendingIntent 会触发 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385096/

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