android - How to update Map via Handler? -


i updating map asynctask. want same handler. looked around couldn't understand should do. asynctask kinda slow , want bit fast.

here doing in asynctask:

    class drawroutetask extends asynctask<geopoint, void, polyline> {      private exception exception;     private polyline roadoverlay;      @override     protected void onpostexecute(polyline polyline) {         super.onpostexecute(polyline);         log.i("asynctask ", " in post execute " + roadoverlay);         roadoverlay = roadoverlay;          // mosmv.invalidate();          mosmv.getoverlays().add(roadoverlay);         mosmv.invalidate();     }      @override     protected polyline doinbackground(geopoint... geopoints) {         try {             log.i("asynctask", "start point >> " + geopoints[0]);             log.i("asynctask", "end point >> " + geopoints[1]);              roadmanager roadmanager = new osrmroadmanager(getcontext());              arraylist<geopoint> waypoints = new arraylist<>();             waypoints.add(geopoints[0]);             waypoints.add(geopoints[1]);               marker marker =  new marker(mosmv);             marker.setposition(geopoints[0]);             marker.setposition(geopoints[1]);              marker.settitle("your location ");             log.i("asynctask ", "drawable >> "+ contextcompat.getdrawable(getcontext(), r.drawable.pin));             marker.seticon(contextcompat.getdrawable(getcontext(), r.drawable.pin));               mosmv.getoverlays().add(marker);              road = roadmanager.getroad(waypoints);              roadoverlay = roadmanager.buildroadoverlay(road);             roadoverlay.setcolor(color.red);               return roadoverlay;           } catch (exception e) {             e.printstacktrace();         }          return null;      } } 

any appreciated.

you mean thread , handler?

do whatever inside doinbackground() function inside thread , send via handler main thread.

your code should this.

    new thread(new runnable(){           public void run(){                 // doinbackground() codes..                 message msg = handler.obtainmessage(what);//what refers field inside message;                 msg.obj = want put;                 msg.sendtotarget(); //dispatches msg handler.           }      }).start();  private handler handler = new handler(){   public void handlemessage(message msg){      //handles message dispatched above code.       int = msg.what; //you can use 'what' switch-case case.      someobject data = (someobject) msg.obj; //   }  } 

note thread's run() or asynctask's doinbackground not ui thread, means changing ui inside these methods cause crashes. but, handlermessage() or onpostexecute() methods performed inside main thread, can use update view.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -