Android-多个不同时间的警报在错误的时间触发
Android-Multiple Alarms with different timings fires at wrong time
我的应用程序中有四个 Repeating Alarms
,每个都在不同的时间触发以执行一些重要任务。
他们是
- 闹钟 1 - 12:30上午
- 闹钟 2 - 01:00上午
- 闹钟 3 - 06:00上午
- 警报 4 - 12:15下午
每个警报都没有问题。每个警报的时间间隔为24 hrs
。但是每次开火的时间都是随机的。
Alarm Set Time Actual Firing Time
Alarm 1 12:30 AM 06:31 AM
Alarm 2 01:00 AM 06:01 AM
Alarm 3 06:00 AM 06:01 AM
Alarm 4 12:15 PM Still not fired 4 hours after that time
我的代码是
闹钟 1
Intent myIntent=new Intent(MainPage.this,db_restore.class);
PendingIntent pi=PendingIntent.getBroadcast(MainPage.this,1,myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
闹钟 2
Intent mor_flag=new Intent(MainPage.this,MorningFlag.class);
PendingIntent mor_intent=PendingIntent.getBroadcast(MainPage.this,3,mor_flag,0);
AlarmManager mor_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
mor_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,mor_intent);
闹钟 3
Intent late_start1=new Intent(MainPage.this,late_start.class);
PendingIntent piLate=PendingIntent.getBroadcast(MainPage.this,2,late_start1,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, piLate);
Toast.makeText(MainPage.this,"Alarm Service started for Late Start monitoring",Toast.LENGTH_LONG).show();
闹钟 4
Intent eve_flag=new Intent(MainPage.this,EveningFlag.class);
PendingIntent eve_intent=PendingIntent.getBroadcast(MainPage.this,4,eve_flag,0);
AlarmManager eve_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM, Calendar.PM);
eve_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,eve_intent);
我不知道我做错了什么。请看看并帮助我。谢谢。
注意: 我正在 API Level 19
Android 4.4.4
中检查这个,最小值为 14。
尝试使用 AlarmManager.setExact()
并给 this post
一些代表
For api levels below 19 you should use AlarmManager.setRepeating()
and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact()
and then when alarm triggers do it again for next week and so on every week.
来自API
文档(https://developer.android.com/training/scheduling/alarms.html)
从 Android 4.4(API 级别 19)开始,所有重复的警报都是不准确的。这意味着 Android 同步来自多个应用程序的重复警报并同时触发它们。
因此,如果您想要一个准确的警报,请使用警报管理器的
setExact (int type, long triggerAtMillis, PendingIntent operation)
方法。
如果你想以准确的时间重复警报,那么你必须编写一个逻辑,你将在其中设置一个警报(不是重复警报),当这个警报响起时,为下一个间隔设置另一个警报。
我的应用程序中有四个 Repeating Alarms
,每个都在不同的时间触发以执行一些重要任务。
他们是
- 闹钟 1 - 12:30上午
- 闹钟 2 - 01:00上午
- 闹钟 3 - 06:00上午
- 警报 4 - 12:15下午
每个警报都没有问题。每个警报的时间间隔为24 hrs
。但是每次开火的时间都是随机的。
Alarm Set Time Actual Firing Time
Alarm 1 12:30 AM 06:31 AM
Alarm 2 01:00 AM 06:01 AM
Alarm 3 06:00 AM 06:01 AM
Alarm 4 12:15 PM Still not fired 4 hours after that time
我的代码是
闹钟 1
Intent myIntent=new Intent(MainPage.this,db_restore.class);
PendingIntent pi=PendingIntent.getBroadcast(MainPage.this,1,myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
闹钟 2
Intent mor_flag=new Intent(MainPage.this,MorningFlag.class);
PendingIntent mor_intent=PendingIntent.getBroadcast(MainPage.this,3,mor_flag,0);
AlarmManager mor_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
mor_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,mor_intent);
闹钟 3
Intent late_start1=new Intent(MainPage.this,late_start.class);
PendingIntent piLate=PendingIntent.getBroadcast(MainPage.this,2,late_start1,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, piLate);
Toast.makeText(MainPage.this,"Alarm Service started for Late Start monitoring",Toast.LENGTH_LONG).show();
闹钟 4
Intent eve_flag=new Intent(MainPage.this,EveningFlag.class);
PendingIntent eve_intent=PendingIntent.getBroadcast(MainPage.this,4,eve_flag,0);
AlarmManager eve_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM, Calendar.PM);
eve_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,eve_intent);
我不知道我做错了什么。请看看并帮助我。谢谢。
注意: 我正在 API Level 19
Android 4.4.4
中检查这个,最小值为 14。
尝试使用 AlarmManager.setExact()
并给 this post
For api levels below 19 you should use
AlarmManager.setRepeating()
and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm withAlarmManager.setExact()
and then when alarm triggers do it again for next week and so on every week.
来自API 文档(https://developer.android.com/training/scheduling/alarms.html)
从 Android 4.4(API 级别 19)开始,所有重复的警报都是不准确的。这意味着 Android 同步来自多个应用程序的重复警报并同时触发它们。
因此,如果您想要一个准确的警报,请使用警报管理器的
setExact (int type, long triggerAtMillis, PendingIntent operation)
方法。 如果你想以准确的时间重复警报,那么你必须编写一个逻辑,你将在其中设置一个警报(不是重复警报),当这个警报响起时,为下一个间隔设置另一个警报。