为什么我的AlarmManager发射瞬间脱落? [英] Why is my AlarmManager firing off instantly?
问题描述
我想建立一个报警程序。我只好报警前的工作,我能够设置不同的时间,报警将适当地熄灭。然后,我改变了ChangeAlarmActivity到TableLayout布局,现在它不会工作?我没碰code。这里是我如何设置报警:
意图alarmIntent =新意图(ChangeAlarmActivity.this,AlarmReceiver.class);
的PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(ChangeAlarmActivity.this,(INT)alarm.getID()
alarmIntent,0);AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
的System.out.println(报警的时间:+小时+:+分);
日历alarmCal = Calendar.getInstance();
//alarmCal.setTimeInMillis(System.currentTimeMillis的());
alarmCal.set(Calendar.HOUR_OF_DAY,小时);
alarmCal.set(Calendar.MINUTE,分钟);
//alarmCal.set(Calendar.SECOND,0);alarmManager.set(AlarmManager.RTC_WAKEUP,
alarmCal.getTimeInMillis(),
pendingAlarmIntent);
可能是因为您的时针和分针是在过去。
让我们说它是16:07小时当前时区(使用24小时时间),且使用小时
为<$执行此code C $ C> 3 和分
为 27
。 03:27在过去,并且因此立即报警的火灾。
让你的两个设置()
通话后,看看时间是比现在更早,如果是这样,添加()
一天。
I'm trying to build an alarm application. I had the alarm working before and I was able to set the different times and the alarm would go off appropriately. I then changed the layout of the ChangeAlarmActivity to a TableLayout and now it won't work? I didn't touch the code. Here is how I set the alarm:
Intent alarmIntent = new Intent(ChangeAlarmActivity.this, AlarmReceiver.class);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(ChangeAlarmActivity.this, (int)alarm.getID(),
alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
System.out.println("Alarm time: " + hour + ":" + min);
Calendar alarmCal = Calendar.getInstance();
//alarmCal.setTimeInMillis(System.currentTimeMillis());
alarmCal.set(Calendar.HOUR_OF_DAY, hour);
alarmCal.set(Calendar.MINUTE, min);
//alarmCal.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarmCal.getTimeInMillis(),
pendingAlarmIntent);
Possibly because your hour and minute are in the past.
Let's say that it is 16:07 hours (using 24 hour time) in your current time zone, and you execute this code with hour
as 3
and min
as 27
. 03:27 was in the past, and hence the alarm fires immediately.
After making your two set()
calls, see if the time is earlier than now, and if so, add()
one day.
这篇关于为什么我的AlarmManager发射瞬间脱落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!