Service timertask stop automatically android -
i have service running in background various timertask periodically interval of 10 mins.
the service works fine until application in foreground , moment app killed timertask behaves strangely example not getting called in every 10 mins.
i saw common issue , been through many answers nothing seems working me. here code snippets.please guide me wrong...
public class myservice extends service { @override public void oncreate() { super.oncreate(); myservicerunningbackground(); service = myservice.this; log.d(tag, "myservicemain"); myservicerunningbackground(); message msgobj = servicehandler.obtainmessage(); servicehandler.sendmessage(msgobj); running = true; } public final handler servicehandler = new handler() { public void handlemessage(message msg) { if(null != servicetimer) { servicetimer.cancel(); servicetimer.purge(); servicetimer = null; } servicetimer = new timer(); servicetimer.scheduleatfixedrate(new myservicemonitorpower(service, target), delay_timer_time, timer_start_time); servicetimer.scheduleatfixedrate(new myservicemonitorlocation(service, target), delay_timer_time, timer_start_time); /* if(target.equals(target.server)) {*/ servicetimer.scheduleatfixedrate(new myservicemonitortrafficstats(service, target), delay_timer_time, timer_start_time); // } } }; @override public int onstartcommand(intent intent, int flags, int startid) { if(intent != null) { settimerinfo(intent.getstringextra("current")); message msgobj = servicehandler.obtainmessage(); servicehandler.sendmessage(msgobj); } return start_sticky; } @override public ibinder onbind(intent intent) { return null; } @override public boolean onunbind(intent intent) { return super.onunbind(intent); } @override public void onrebind(intent intent) { super.onrebind(intent); } @override public void ondestroy() { super.ondestroy(); running = false; } private void settimerinfo(string check) { if(check != null) { log.d(tag, "check "+ check); if (check.equals("enable")) { // alarm.setalarm(service, 5000); target = target.device; delay_timer_time = 0; timer_start_time = 5000; } if (check.equals("disable")) { // alarm.setalarm(service, 600000); myservicerunningbackground(); target = target.server; delay_timer_time = 300000; timer_start_time = 600000; } } } private void myservicerunningbackground() { log.d(tag,"esservcie "); final int restartalarminterval = 600000; final int resetalarmtimer = 2*30*1000; final intent restartintent = new intent(this, myservice.class); restartintent.putextra("alarm_restart_service_died", true); final alarmmanager alarmmgr = (alarmmanager)getsystemservice(context.alarm_service); handler restartservicehandler = new handler() { @override public void handlemessage(message msg) { pendingintent pintent = pendingintent.getservice(getapplicationcontext(), 0, restartintent, 0); // alarmmgr.set(alarmmanager.elapsed_realtime, systemclock.elapsedrealtime() + restartalarminterval, pintent); alarmmgr.setrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), 1000 * 60 * 10, pintent); sendemptymessagedelayed(0, resetalarmtimer); } }; restartservicehandler.sendemptymessagedelayed(0, 0); } public enum target { server, device } private static boolean running; public timer servicetimer; private static myservice service;
static target target = null;
public int delay_timer_time = 60000; public int timer_start_time = 600000; private static final string tag = "myservice";
} using alarm manager well, not seems working. want 3 timertask should kept running when application in background has period of 10 mins.
to start , change mode of services 5 second 10 mins use below code activity
private void startservices() { log.d(tag, "startservices "); intent intent = new intent(myactivity.this, myservice.class); intent.putextra("current", "enable"); //disable 10 mins on ondestroy startservice(intent); }
thanks suggestions
you need initialize 3 timer
s different schedule. snippet:
servicetimer = new timer(); servicetimer.scheduleatfixedrate(new myservicemonitorpower(service, target), delay_timer_time, timer_start_time); servicetimer.scheduleatfixedrate(new myservicemonitorlocation(service, target), delay_timer_time, timer_start_time); servicetimer.scheduleatfixedrate(new myservicemonitortrafficstats(service, target), delay_timer_time, timer_start_time);
we can know servicetimer
confusing, timertask
scheduled. solution is, make sure initialize 3 timer
s:
servicetimer = new timer(); timer2 = new timer(); timer3 = new timer(); servicetimer.scheduleatfixedrate(new myservicemonitorpower(service, target), delay_timer_time, timer_start_time); timer2.scheduleatfixedrate(new myservicemonitorlocation(service, target), delay_timer_time, timer_start_time); timer3.scheduleatfixedrate(new myservicemonitortrafficstats(service, target), delay_timer_time, timer_start_time);
and cancel them once service
destroyed:
@override public void ondestroy(){ running = false; servicetimer.cancel(); timer2.cancel(); timer3.cancel(); super.ondestroy(); }
using alarmmanager
short time task may drains user's battery, use timer
instead.
Comments
Post a Comment