Android:AlarmManager在过去的时间里触发

编程入门 行业动态 更新时间:2024-10-20 18:52:21
本文介绍了Android:AlarmManager在过去的时间里触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

This is my code for alarm manager:

Intent intent=new Intent(getBaseContext(),AlarmReciever.class); intent.setAction("com.example.projectx.ACTION"); PendingIntent pendingIntent=PendingIntent.getBroadcast(this,12345, intent,PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarmManager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),pendingIntent);

如果我选择了在未来一小时/分钟触发的闹钟,此代码将非常有用。但是,如果我选择了过去的一个小时/分钟,则当我单击设置警报时,它会立即启动。

The code works great if i select the alarm to fire off at a future hour/minute. But if I select a past hour/minute it fires up immediately as I click on "set alarm".

示例:

  • 现在是15:00,我将闹钟设置为15:45,闹钟在15:45响起,一切正常

  • now it is 15:00 , i set the alarm for 15:45, alarm goes off at 15:45, everything ok

现在是15:00,我将闹钟设置为14:30,只要单击设置闹钟,闹钟就会响起!

now it is 15:00 , i set the alarm for 14:30, the alarm goes off as soon as i click "set alarm"!

我的时间选择器始终设置为24小时模式。

My time picker is always set to 24 hour mode. Could that be a problem?

谢谢!

推荐答案

当然可以了。它应该是这样工作的。 Android意识到时间已经过去了,因此即使时间晚了,也会发出警报。

Of course it does... It's meant to work so. Android recognizes that the time is past, so it will fire the alarm, even if it's late.

您可以确保为闹钟设置的时间在当前时间之后。 只需计算此差异即可:

You can make sure that the time set for the alarm is after the current time. Just calculate this difference:

int diff = Calendar.getInstance().getTimeInMilis() - targetCal.getTimeInMillis();

如果差异大于0,则在日历中添加一天(targetCal) 现在,您设备的时间将比下一个排定的闹钟时间更早(而不是更早)。

If diff is greater than 0, then add a day to your calendar (targetCal) Now, your device's time will be earlier (instead of being later) than the next scheduled alarm time.

更多推荐

Android:AlarmManager在过去的时间里触发

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

发布评论

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

>www.elefans.com

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