java - Having trouble making a Android.os.Handler continue running -
so, have service need keep running in order send ayt device , report on connection (basically whether it's or down) @ regular intervals. have setup right now, works, after 10 or 15 minutes, ceases run. it's using handlerthread, handler , runnable start service, work, stops itself.
mainactivity.java snippets:
private handlerthread hthread = new handlerthread("maintainconnection"); private handler h; private runnable htask = new runnable() { @override public void run() { startservice(new intent(getapplicationcontext(), maintainconnectionservice.class)); h.postdelayed(this, 2000); } }; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); hthread.start(); h = new handler(hthread.getlooper()); /* other code goes here */ } @override protected void onpause() { super.onpause(); log.v(tag, "i in onpause"); h.removecallbacks(htask); } @override protected void onresume() { super.onresume(); log.v(tag, "i in onresume"); h.post(htask); } @override protected void onstop() { super.onstop(); log.v(tag, "i in onstop"); h.removecallbacks(htask); }
maintainconnectionservice.java
public class maintainconnectionservice extends intentservice { public maintainconnectionservice() { super(maintainconnectionservice.class.getname()); } private static final string tag = "maintainconnectionsvc"; public static handler uihandler; static { uihandler = new handler(looper.getmainlooper()); } public static void runonui(runnable runnable) { uihandler.post(runnable); } @override protected void onhandleintent(intent intent) { try { if (comms.telnet.sendayt(comms.ayt_timeout)) { log.d(tag, "sent ayt => " + comms.telnet.getremoteaddress() + ":" + comms.telnet.getremoteport()); runonui(new runnable() { public void run() { colourcontrolfragment.connstat.setbackgroundcolor(color.green); colourcontrolfragment.connstattxt.settext("connection established"); } }); } else { runonui(new runnable() { public void run() { colourcontrolfragment.connstat.setbackgroundcolor(color.yellow); colourcontrolfragment.connstattxt.settext("attempting reconnect"); } }); } } catch (exception e) { log.e(tag, "failed send ayt to: " + comms.telnet_address); runonui(new runnable() { public void run() { colourcontrolfragment.connstat.setbackgroundcolor(color.red); colourcontrolfragment.connstattxt.settext("not connected"); } }); startservice(new intent(getapplicationcontext(), establishconnectionservice.class)); } stopself(); } }
it appears work expected, said, after around 10 or 15 minutes, no warning, goes "yellow" state (attempting reconnect) , stays there. if close app , restart it, resumes working.
any way prevent threads being killed? suggestions on better way monitor connection?
i've tried plain thread
s , timer
s, no avail.
the handler in activity should static, otherwise have risk of leaking activity.
also, if remove callbacks in onresume , onpause (doing in both not make sense btw), make sense no longer receiving messages.
Comments
Post a Comment