android - Not receiving CountDownTimer callbacks in Service -
i have bound service collects locations 3 minutes every 15 minutes. start countdowntimer 1 time service connected in onserviceconnected
of serviceconnection
. receive timer callbacks (onfinish
) (ontick
) far activity bindservice
visible. when device locked not receive updates timer.
mylocationservice
public class mylocationservice extends service implements mytimerlistener{ private ibinder mbinder = new mylocationbinder(); public void oncreate() { locationmanager = new mylocationmanager(this); } @override public ibinder onbind(intent arg0) { homecoming mbinder; } public class mylocationbinder extends binder { public mylocationservice getservice() { homecoming mylocationservice.this; } } public void startfetchinglocations() { if(locationmanager != null){ locationmanager.startfetchinglocations(); if(publishperiodcountdowntimer != null) { publishperiodcountdowntimer.cancel(); publishperiodcountdowntimer = null; } publishperiodcountdowntimer = new mycounttimer(gpspublishperiod * 60 * 1000, 1000, mylocationservice.this); publishperiodcountdowntimer.start(); } } @override public void ontimefinished() { //start fetching locations locationmanager.startfetchinglocations(); //start 3 min countdowntimer } @override public void ontick() { } public void onalllocationsrecieved(arraylist<location> locations) { //do stuff on locations locationmanager.stopfetchinggps(); //stops 3 min countdowntimer } }
myactivty
public class myactivity { @override protected void onstart() { super.onstart(); btnstop.setvisibility(view.visible); mynoificationmanager.cancelallnotifications(); if (!islocationservicebound) { intent locserviceintent = new intent(this, mylocationservice.class); bindservice(locserviceintent, locationserviceconnection, context.bind_auto_create); } } private serviceconnection locationserviceconnection = new serviceconnection(){ @override public void onservicedisconnected(componentname name) { islocationservicebound = false; } @override public void onserviceconnected(componentname name, ibinder service) { mylocationbinder locationbinder = (mylocationbinder) service; locationservice = locationbinder.getservice(); islocationservicebound = true; locationservice.startfetchinglocations(); } }; }
it works expected when activity visible. timer doesn't provide ontick()
or ontimefinished()
callbacks when device locked. problem here ?
the countdowntimer not work when phone goes sleep mode. design , it's save battery life.
alternately can utilize android.app.alarmmanager instead accomplish goal. next sample code how that. set alaram below
alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); intent intent = new intent(this, mylocationservice.class); intent.putextra("need_to_fetch_loc", true); pendingintent alarmintent = pendingintent.getbroadcast(this, 0, intent, 0); alarmmanager.set(alarmmanager.rtc_wakeup, gpspublishperiod * 60 * 1000, alarmintent);
add next method mylocationservice class
@override public int onstartcommand(intent intent, int flags, int startid) { if(locationmanager!= null && intent.getbooleanextra("need_to_fetch_loc", false)) { locationmanager.startfetchinglocations(); } homecoming super.onstartcommand(intent, flags, startid); }
android service countdowntimer
No comments:
Post a Comment