手动更改系统时间时如何停止Android AlarmManager?

编程入门 行业动态 更新时间:2024-10-24 07:28:16
本文介绍了手动更改系统时间时如何停止Android AlarmManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这里是问题的扩展示例。现在是1:30 AM。警报设置为今天晚些时候,上午10点。我昨天将手机时间更改为1159PM。警报管理器会在错误的时间立即触发。

Here is an extended example of the problem. It is 1:30AM. Alarm is set for later today, at 10AM. I change my phones time to 1159PM yesterday. Alarm Manager instantly triggers, at the wrong time.

如果时间已过去,如何阻止警报管理器响铃?

How do I stop the AlarmManager from ringing if the time is in the past?

但是,如果我更改时间,则什么也不会发生。

If however, I change the time forward, nothing happens.

我不希望这种情况发生在我的应用程序中,因为其他警报应用程序不会发生这种情况。有没有一种方法可以确保警报管理器仅在设置的确切时间响铃(如果不跳过它)?

I do not want this to happen with my app, as it does not happen with other Alarm apps. Is there a way to make sure that the Alarm manager only rings at the exact minute it is set (if not skip it)?

这是我的警报管理器代码。

Here is my Alarm Manager code.

Calendar alarmCal = closestAlarm.toCalendar(); Intent alarmIntent = new Intent(this, ActiveAlarmService.class); alarmIntent.putExtra("alarm", closest); pendingAlarm = PendingIntent.getService(AlarmService.this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT); alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCal.getTimeInMillis(), pendingAlarm);

我尝试使用BroadcasetReceiver捕获时间/日期更改事件,但是滞后时间太多

I have tried using a BroadcasetReceiver to catch the Time/Date change events, but there is too much lag between the broadcast, and the AlarmManager firing because it is set in the past.

推荐答案

我已经找到了解决方法。感觉很奇怪,但是很扎实。

I have found the solution. It feels odd, but it is solid.

这是我最初使用的内容。

Here is what I was using originally.

alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimeCalendar.getTimeInMillis(), pendingAlarmIntent);

这就是解决方案。

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTimeCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingAlarmIntent);

您必须像管理第一种方法一样管理和取消此AlarmManager。如果您忘记了,闹钟会在不应该响起时响起。

You will have to manage and cancel this AlarmManager as if it was the first method. If you forget, your alarm will ring when it should not.

更多推荐

手动更改系统时间时如何停止Android AlarmManager?

本文发布于:2023-11-27 10:21:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   系统   AlarmManager   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!