Android: Resetting repeating AlarmManager from within called service -
i've set repeating alarm on service , decided it's convenient reset alarm within called service. reason service has code check if it's within user-defined schedule (time range). when it's outside time range, resets alarm start @ future time selected user. maybe i'm approaching wrong i'll put question out there , see think.
an activity kicks off service creating repeating alarm:
//activity calendar cal = calendar.getinstance(); intent intent = new intent(getapplicationcontext(), myservice.class); intent.setdata(uri.parse("myservice://identifier")); pendingintent pintent = pendingintent.getservice(getapplicationcontext(), 0, intent, 0); alarmmanager alarm = (alarmmanager)getsystemservice(alarm_service); alarm.setinexactrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), intervalinmins*60000, pintent);
the service has this:
//service @override public int onstartcommand(intent intent, int flags, int startid) { uri action = intent.getdata(); try { if (action.equals(uri.parse("myservice://identifier"))) { //simplifying code here: calculatedoffset determined current time //and scheduled start time. intervalinmins read settings. if (!withinschedule()) { calendar cal = calendar.getinstance(); pendingintent pintent = pendingintent.getservice(getapplicationcontext(), 0, intent, 0); alarmmanager alarm = (alarmmanager)getsystemservice(alarm_service); alarm.setinexactrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis() + calculatedoffset, intervalinmins*60000, pintent); } } } catch (nullpointerexception np) { np.printstacktrace(); } return service.start_redeliver_intent; }
i hoping re-use intent reset repeating alarm. new code, i'm seeing multiple alarms stack firing rapidly in succession around when start time hits. should not spaz out that, should fire @ regular intervals did before scheduling reset. need catch in debugger haven't been able determine exact conditions yet. understanding of alarms off base here? there better way this?
addendum: wrinkle in i'm using roottools gain superuser privileges in order work around android 4.2's airplane mode. hasn't been problem before scheduling, i'm suspicious whether su blocking long time while alarms stack up.
re-using intent inside service receives alarm work. i've switched using repeating alarm single-shot alarm gets re-armed every time service called. unfortunately didn't fix problem of alarms stacking. culprit su blocking. may roottools or su itself. need update library 2.6 3.x , see if makes difference.
Comments
Post a Comment