Android : Alarm Manager setRepeating firing randomly

on Saturday, August 16, 2014


I've read through most of the posts here about setRepeating alarms, and none seem to fix my issue.


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. 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.


For example when I set both repeating to be exactly one minute after each other:


17:09:20 -- Start alarm manager

17:09:35 -- Both repeating alarm fired

17:10:23 -- One fired

17:11:28 -- One fired

17:12:34 -- One fired, etc...


QUESTION: How can I dismiss these random notifications appearing after ~10 seconds?


0 comments:

Post a Comment