Android: Sticky Service not restarting -
my app uses service background tasks. want service keep running when user kills app(swipes away). there 2 scenario's in user kan kill app:
scenario 1: when in app: 1 user presses **backbutton**, apps goes background , user on homescreen. 2 user presses multitasking button , swips app away. 3 service should restart because sticky. scenario 2: when in app: 1 user presses **homebutton**, apps goes background , user on homescreen. 2 user presses multitasking button , swips app away. 3 service should restart because sticky.
scenario 1 works expected. service restarts after app swiped away in matter of seconds.
scenario 2 not work expected. service restarted after more 5 minutes.
i don't understand why takes long restart service in scenario 2. there known solution this?
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent test = new intent(this, testservice.class); startservice(test); } } public class testservice extends service { @override public int onstartcommand(intent intent, int flags, int startid) { log.e("service", "service restarted!");//in scenario 2, takes more 5 minutes print this. return service.start_sticky; } @override public ibinder onbind(intent intent) { return null; } }
when press button, destroying activity means ondestroy()
called stop()
sticky_service
. hence sticky_service boots again immediately.
when press home button, pausing activity (onpause()
), putting in background. activity destroyed when os decides gc it. @ point of time ondestroy()
called @ stop()
service , sticky_service
boots again.
hope helps.
Comments
Post a Comment