[英]Alarm Manager setRepeating firing randomly
I've read through most of the posts here about setRepeating alarms, and none seem to fix my issue. 我已经阅读了有关setRepeating警报的大多数帖子,似乎都无法解决我的问题。
I have two repeating alarms set for a set amount of times as seen here: 我将两个重复的警报设置为一定的时间,如下所示:
trigger = System.currentTimeMillis()
+ (Integer.parseInt(test) * 60 * 1000);
workLength = (long) (Integer.parseInt(test) * 60 * 1000);
breakLeng = (long) (Integer.parseInt(breakLength) * 60 * 1000);
private void recurringInitialAlarm() {
// TODO Auto-generated method stub
// work inital
alarm.set(AlarmManager.RTC_WAKEUP, trigger, pintent);
SharedPreferences pref = getActivity()
.getSharedPreferences("pref", 0);
SharedPreferences.Editor edit = pref.edit();
System.out.println("initial work alarm set");
edit.putString("takeBreak", "true");
edit.commit();
System.out.println("takeBreak = true");
}
private void recurringWorkAlarm() {
// TODO Auto-generated method stub
// work recurring
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), (2 * workLength) + breakLeng,
pintent3);
System.out.println("work recurring"
+ ((2 * workLength) + breakLeng));
}
private void recurringBreakAlarm() {
// TODO Auto-generated method stub
// break
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), workLength + breakLeng, pintent2);
System.out.println("break times"
+ workLength + breakLeng);
}
where the pending intents are specified by: 其中待定意图由以下方式指定:
Intent intent = new Intent(getActivity(), AlarmReceiver.class);
final PendingIntent pintent = PendingIntent
.getBroadcast(getActivity(), 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent pintent2 = PendingIntent
.getBroadcast(getActivity(), 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent pintent3 = PendingIntent
.getBroadcast(getActivity(), 3, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
The problem is that the two repeating alarms fire about 10 seconds after creation, and times in between randomly. 问题在于,两个重复的警报在创建后约10秒钟后触发,并且间隔时间是随机的。 I understand that for KitKat devices, the alarms are not exact and can vary, however, it does properly send a notification around the times afterwards in the long run.
我知道对于KitKat设备,警报并不准确,并且可能会有所不同,但是,从长远来看,它确实会在以后的某个时间发送通知。
For example when I set both repeating to be exactly one minute after each other: 例如,当我将两个重复设置为彼此精确地相隔一分钟时:
17:09:20 -- Start alarm manager 17:09:20-启动警报管理器
17:09:35 -- Both repeating alarm fired 17:09:35-重复发出警报
17:10:23 -- One fired 17:10:23-一个被解雇
17:11:28 -- One fired 17:11:28-一个被解雇了
17:12:34 -- One fired, etc... 17:12:34-一被解雇,等等...
QUESTION: How can I dismiss these random notifications appearing after ~10 seconds? 问题:如何消除大约10秒后出现的这些随机通知?
The reason the alarms are firing "immediately" (after 15 seconds) is that you schedule the first event for System.currentTimeMillis()
- and the AlarmManager does it within 15 seconds which is pretty close. 警报“立即”(在15秒后)触发的原因是,您计划了
System.currentTimeMillis()
的第一个事件-AlarmManager在15秒之内完成了它,这非常接近。
You need to do something like set the first alarm event for System.currentTimeMillis() + initial_wait_period
您需要执行类似设置
System.currentTimeMillis() + initial_wait_period
的第一个警报事件的操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.