android - why is my HandlerThread updating the UI -


i practicing handlerthread in android easy camera example - problem if create cameracontroller object in oncreate , send message handler thread in order process opencamera() method ( contains direct ui update code)

it allowing me updated ui although shouldn't allowed - since technically i`m supposed in different thread.

if move code oncheckedchanged method (it commented in below snippet) application crash saying trying update ui thread different thread.

public class mainactivity extends appcompatactivity implements handler.callback {  private togglebutton togglebutton; private textview textview; private cameracontroller mtestcam;  //generate messages private static final int msg_open_cam = 0; private static final int msg_close_cam = 1; private static final int msg_open_flash= 2; private static final int msg_close_flash = 3; //verify private static final int msg_open_cam_done = 4; // nu trebuie private static final int msg_close_cam_done = 5; private static final int msg_open_flash_done= 6; private static final int msg_close_flash_done= 7;  //used mainactivity handlemessage protected handler mhandler;  //camera controler thread private cameracontrollerthread mcameracontrolthread;    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       //android marshmallow - in order service run need runtime permissions     //testcam.opencamera(); //moved method      togglebutton = (togglebutton) findviewbyid(r.id.togglebutton);     textview = (textview) findviewbyid(r.id.textview_text);      mhandler = new handler(this);     mcameracontrolthread = new cameracontrollerthread("camera control thread");     mcameracontrolthread.start();      //moved onstart     mtestcam = new cameracontroller();     message opencameramsg = mcameracontrolthread.mworkerhandler.obtainmessage(this.msg_open_cam);     opencameramsg.sendtotarget();       togglebutton.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {         @override         public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) {              if(ischecked){                  mtestcam = new cameracontroller();                 message opencameramsg = mcameracontrolthread.mworkerhandler                         .obtainmessage(msg_open_cam);                 opencameramsg.sendtotarget();                  //mtestcam.openflash();             } else {                 //close flash                 //mtestcam.stopflash();             }          }     });  }  private void opencamera(){     synchronized (mtestcam){         mtestcam.opencamera();     }     toast.maketext(this, "this text", toast.length_long).show();     textview.setvisibility(view.visible);     textview.settext("camera open"); }  @override public boolean handlemessage(message message) {     return false; }   private class cameracontrollerthread extends handlerthread implements handler.callback{      protected handler mworkerhandler;      public cameracontrollerthread(string name){         super(name);     }      @override     protected void onlooperprepared() {         mworkerhandler = new handler(getlooper(), this);     }      @override     public boolean handlemessage(message message) {          switch(message.what){             case msg_open_cam:                 opencamera();                 break;         }          return true;     }  } } 

i guess question - why if instantiate cameracontroller , send message oncreate - allowing me update ui - , when same thing oncheckedchangelistener not allow me?

quoting android documentation:

[accessing android ui toolkit outside ui thread] can result in undefined , unexpected behavior, can difficult , time-consuming track down.

anything can happen after race condition.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -