Android repeating alarm not repeating correctly -
i have alarm wanting repeat around every 5 minutes. testing purposes, have set repeat once every 5 seconds instead, such:
alarmmanager alarmmanager = (alarmmanager)getactivity().getsystemservice(getactivity().alarm_service); intent intent = new intent(getactivity(), coordinatealarmreceiver.class); pendingintent pendingintent = pendingintent.getservice(getactivity(), 1, intent, 0); int repeatseconds = 5; alarmmanager.setinexactrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), repeatseconds * 1000, pendingintent);
and receiving intentservice prints log statement when receives alarm. however, fires around once every minute , half instead of once every 5 seconds, set incorrectly? have tried using setrepeating() instead of setinexactrepeating() same results.
here alarm receiver:
public class coordinatealarmreceiver extends intentservice { public coordinatealarmreceiver(){ super("coordinatealarmreceiver"); } /* alarm received, new contacts shows notification */ @override protected void onhandleintent(intent intent) { mylog.i("coordinate alarm received"); //new getnewcontactstask(this).execute(); } }
i had similar problem in needed service fired every 15 seconds... did following.
i have class extends application called myapplication. class holds instance of alarm manager.
public class myapplication extends application { private myalarmmanager alarmmgr; @override public void oncreate() { log.d(tag, "myapplication oncreate"); super.oncreate(); log.d(tag, "initing alarmmgr ..."); alarmmgr = new myalarmmanager(this); } public myalarmmanager getalarmmanager(){ return alarmmgr; } }
an alarmmanager called myalarmmanager creates, starts & stops alarms and now sets next alarm 1 service.
public class myalarmmanager { private myapplication mapp; private intent intent; private pendingintent pendingintent; private alarmmanager alarmmgr; private static final long frequency = 15000; public myalarmmanager(context context) { mapp = (myapplication) context; // android alarm service alarmmgr = (alarmmanager) context.getsystemservice(context.alarm_service); // service fired every 15 seconds intent = new intent(context, myservice.class); pendingintent = pendingintent.getservice(context, 1, intent, pendingintent.flag_update_current); setnextalarm(); } public void setnextalarm(){ log.d(tag, "setting next alarm..."); alarmmgr.set(alarmmanager.rtc_wakeup, (system.currenttimemillis() + frequency), pendingintent); } private void stopalarms(){ log.d(tag, "stopping alarms"); alarmmgr.cancel(pendingintent); } }
when service fired instance of myapplication, alarmmanager , schedule next alarm.
public class myservice extends service { myapplication mapp; @override public int onstartcommand(intent intent, int flags, int startid) { log.d(tag, "in onstartcommand"); // init app reference if needed if (mapp == null) { log.d(tag, "app null. creating now..."); mapp = (myapplication) getapplicationcontext(); } // schedule next zone check mapp.getalarmmgr().setnextalarm(); // custom functionality ... }
Comments
Post a Comment