AlarmManager 警报在触发时间过去时立即调用
AlarmManager alarm is called immediately when trigger time is in the past
下面是我用来在从外部提取数据时创建警报的代码 API。如果设置的时间是过去的时间,则闹钟会在设置后立即响起(间隔 2 秒)。例如,如果我将闹钟设置为 4 月 10 日 8:00 AM,即 4 月 11 日 10:00 AM(过去时间)。设置后立即启动闹钟。
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);
}
}
我希望闹钟在下次到达时间时响起。我哪里出错了?
这是预期的行为。
来自 setRepeating()
(和其他 AlarmManager
设置方法)的文档:
If the stated trigger time is in the past, the alarm will be triggered
immediately
如果您想防止这种情况发生,那么只需不要设置具有过去触发时间的警报(例如,在设置警报时检查 System.currentTimeMillis()
)。
好吧,我 运行 遇到了同样的问题,经过研究我发现当闹钟设置为过去时间时,闹钟会 运行 。
资料来源:Here is documentation of Alarm Manager - setRepeating()
所以,我通过检查是否 "Calendar time is in past from system time than I add a day"
解决了这个问题
工作代码:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.cancel(pendingIntent);
// Check if the Calendar time is in the past
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
Log.e("setAlarm","time is in past");
calendar.add(Calendar.DAY_OF_YEAR, 1); // it will tell to run to next day
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); //Repeat every 24 hours
下面是我用来在从外部提取数据时创建警报的代码 API。如果设置的时间是过去的时间,则闹钟会在设置后立即响起(间隔 2 秒)。例如,如果我将闹钟设置为 4 月 10 日 8:00 AM,即 4 月 11 日 10:00 AM(过去时间)。设置后立即启动闹钟。
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);
}
}
我希望闹钟在下次到达时间时响起。我哪里出错了?
这是预期的行为。
来自 setRepeating()
(和其他 AlarmManager
设置方法)的文档:
If the stated trigger time is in the past, the alarm will be triggered immediately
如果您想防止这种情况发生,那么只需不要设置具有过去触发时间的警报(例如,在设置警报时检查 System.currentTimeMillis()
)。
好吧,我 运行 遇到了同样的问题,经过研究我发现当闹钟设置为过去时间时,闹钟会 运行 。 资料来源:Here is documentation of Alarm Manager - setRepeating()
所以,我通过检查是否 "Calendar time is in past from system time than I add a day"
解决了这个问题工作代码:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.cancel(pendingIntent);
// Check if the Calendar time is in the past
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
Log.e("setAlarm","time is in past");
calendar.add(Calendar.DAY_OF_YEAR, 1); // it will tell to run to next day
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); //Repeat every 24 hours