触发时间过去后立即调用AlarmManager警报 [英] AlarmManager alarm is called immediately when trigger time is in the past

查看:92
本文介绍了触发时间过去后立即调用AlarmManager警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是当从外部API提取数据时我用来创建警报的代码。如果设定的时间是过去的时间,则闹铃会在设定的时间(2秒间隔)后立即响起。例如,如果我将警报设置为4月10日(过去时间)4月10日上午8:00,上午10:00。

Below is the code I am using to create alarms when data is pulled from external API. If the time set is in the past, the alarm goes off as soon as it is set(2 second gap). For example if I set the alarm for 8:00 AM, 10th April at 10:00 AM on 11th April(Past time). It starts the alarm as soon as it is set.

public static final int ALARM_REQUEST_CODE = 1001;
public static AlarmManager alarmManager = (AlarmManager) EHCApplication.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
public static Intent alarmIntent = new Intent(EHCApplication.getInstance().getApplicationContext(), AlarmReceiver.class);
public static PendingIntent pendingIntent = PendingIntent.getBroadcast(EHCApplication.getInstance().getApplicationContext(), ALARM_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);



public static void setAlarm(Reminder rm) {
    for (ScheduledTime time : rm.getScheduledTime()) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constants.ARGS_SELECTED_MEDICINE, medicine);
        alarmIntent.putExtras(bundle);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMilliseconds(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

我希望闹钟从下一次响起时间到了。我在哪里弄错了?

I am expecting the alarm to go off from next time it hit the time. Where am I making mistake?

推荐答案

这是预期的行为。

来自 setRepeating() (以及其他 AlarmManager 设置方法):


如果指定的触发时间已过去,警报将立即触发

If the stated trigger time is in the past, the alarm will be triggered immediately

如果您想防止这种情况发生,那就不要设置触发时间过去的警报(例如,检查 System.currentTimeMillis()设置闹钟)。

If you would like to prevent that happening, then simply do not set alarms with a past trigger time (e.g. check against System.currentTimeMillis() when setting the alarm).

这篇关于触发时间过去后立即调用AlarmManager警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆