java - android app freezes from code running in worker thread -


i want start periodic event when user's location change. have location listener starts thread , thread post runnable handler

public class gpslistener implements locationlistener { private string tag = "gpslistener"; private refreshthread runnable = null; private thread thread = null; private context context = null;  public gpslistener(context context) {     // this.maphandler = maphandler;     this.context = context; }  @override public void onlocationchanged(location location) {     // todo auto-generated method stub     log.i(tag, "onlocationchanged");     log.i(tag, "latitude: " + location.getlatitude() + "\n" + "longitude: "             + location.getlongitude());      testthread t1 = new testthread(location.getlatitude(),             location.getlongitude(), context);     thread t = new thread(t1);     t.start();      } }   public class testthread implements runnable { private handler handler = new handler(); private refreshthread thread = null;// new refreshthread(); private double latitude; private double longitude; private context context; private string tag = "testthread";  public testthread(double latitude, double longitude, context context) {     this.latitude = latitude;     this.longitude = longitude;     this.context = context; }  @override public void run() {     // todo auto-generated method stub     log.i(tag, "run");     thread = new refreshthread(latitude, longitude, context, handler);     handler.removecallbacks(thread);     handler.post(thread);     } 

}

when thread start want keep posting same runnable until testthread called again (i'm not stopping testthread in location listener not problem right now)

public class refreshthread implements runnable {  private double latitude; private double longitude; private handler handler = null; private string tag = "refreshtread"; private int time = 10000; private context context = null;  public refreshthread(double latitude, double longitude, context context,         handler handler) {// ,     // maphandler     // maphandler) {     this.longitude = longitude;     this.latitude = latitude;     // this.maphandler = maphandler;     this.handler = handler;     this.context = context; }  @override public void run() {     log.i(tag, "run");     // while (true) {     log.i(tag, "doing work");     getmapdatatask task = new getmapdatatask();     task.execute("asfasf#123", longitude + "#" + latitude);     try {         string response = task.get();         log.i(tag, response);         bundle data = new bundle();         data.putdouble("longitude", longitude);         data.putdouble("latitude", latitude);         intent intent = new intent("updatemap");         arraylist<point> points = new arraylist<point>();         if (response != null && !response.equals("no poi")) {             arraylist<string> temppoints = new arraylist<string>();             while (response.contains("$")) {                 string[] tempar = response.split("\\$", 2);                 temppoints.add(tempar[0]);                 response = tempar[1];             }             temppoints.add(response);              (string temp : temppoints) {                 string[] tempar = temp.split("#");                 point temppoint = new point(double.parsedouble(tempar[1]),                         double.parsedouble(tempar[0]), tempar[2], tempar[3]);                 points.add(temppoint);             }             // msg.obj = points;         } else             points = null;         intent.putextra("data", data);         intent.putextra("points", points);         localbroadcastmanager.getinstance(context).sendbroadcast(intent);         // msg.obj = null;         // maphandler.sendmessage(msg);         // log.i(tag, "finished work, going sleep");     } catch (interruptedexception e) {         log.i(tag, "interrupted");         e.printstacktrace();         return;         // todo auto-generated catch block     } catch (executionexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     // }     // todo auto-generated method stub     handler.postdelayed(this, time);  } 

}

and refreshthrad start asynctask tries connect web service

public class getmapdatatask extends asynctask<string, object, string> {  private final string method_name = "getmapdata"; private final string namespace = "http://server/"; private final string wsdl_url = "http://192.168.2.2:9999/poi/poiservice?"; private string tag = "getmapdatatask"; private final string soap_action = "\"http://server/getmapdata\"";  @override protected string doinbackground(string... params) {     // todo auto-generated method stub     log.i(tag, "doinbackground");     log.i(tag, params[0] + "," + params[1]);     // string userinfo = params[0];     soapobject parameter = new soapobject(namespace, method_name);     parameter.addproperty("user_data", params[0]);     parameter.addproperty("position", params[1]);     soapserializationenvelope envelope = new soapserializationenvelope(             soapenvelope.ver11);     envelope.setoutputsoapobject(parameter);      // 3. create http transport object send web service request     httptransportse httptransport = new httptransportse(wsdl_url, 30000);     httptransport.debug = true; // allows capture of raw request/respose in                                 // logcat      // 4. make web service invocation     try {         httptransport.call(soap_action, envelope);     } catch (httpresponseexception e) {         log.i(tag, "error1");         // todo auto-generated catch block         e.printstacktrace();     } catch (ioexception e) {         log.i(tag, "error2");         e.printstacktrace();         return "error while connectiong server";         // todo auto-generated catch block     } catch (xmlpullparserexception e) {         log.i(tag, "error3");         // todo auto-generated catch block         e.printstacktrace();     }      // logging raw request , response (for debugging purposes)     log.d(tag, "http request:\n" + httptransport.requestdump);     log.d(tag, "http response:\n" + httptransport.responsedump);     string processedresponse = null;     // 5. process web service response     if (envelope.bodyin instanceof soapobject) { // soapobject = success         soapobject soapobject = (soapobject) envelope.bodyin;         processedresponse = parsesoapresponse(soapobject);         // ... whatever want object     } else if (envelope.bodyin instanceof soapfault) { // soapfault =failure         soapfault soapfault = (soapfault) envelope.bodyin;         processedresponse = soapfault.faultstring;         try {             throw new exception(soapfault.getmessage());         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }      return processedresponse; } } 

first tried instead of test thread use service application hanging @ httptransport.call(soap_action, envelope); if service not available(i didn't start it) 30 seconds because of httptransportse httptransport = new httptransportse(wsdl_url, 30000); tried use testthread not happening. does.

can please explain why code hangs application?

your handler runs in ui thread got created.

your asynctask runs in own thread call get() on it, makes ui thread (the thread of handler) wait until end of doinbackground()

this messy way of threading, why not run asynctask?


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -