java - Update a "checkBox" from a non-UI thread -


i've coded "battleship" program school project, , decided make portable, mobile version of program. now, i'm done, problem have left want checkbox (obviously on ui thread) display if it's players turn @ given moment. in desktop version, used in separate thread, don't block stuff on main thread. code following:

public void run() {         while (true) {             if (clientcurrentplayer.currentplayer == battleshipmain.myplayernumber) {                 battleshipmain.yourturn.setchecked(true);             } else if (clientcurrentplayer.currentplayer != battleshipmain.myplayernumber) {                 battleshipmain.yourturn.setchecked(false);             }             try {                 thread.sleep(10);             } catch (interruptedexception e) {                 e.printstacktrace();             }         }     } 

however, returns exception, stating cannot update view other threads 1 created view. after bit of research, seems solve runonuithread. don't how i'm supposed that, though - if run code on ui thread, block input, right? , can't run code on separate thread , perform setchecked part in method in ui thread, since either runs method on wrong thread, or can't call method since runonuithreadcannot used in static context. don't it, please me ._. in advance, though! :)

rather polling check this, consider using event driven mechanism. threads can simple wait on events come in. ui thread doing looper bound behind scenes , can leverage creating handler on main thread, attaching own handler.callback handler , sending messages. or, can send runnable instance handler.

polled threads appealing, run problems seeing , can lead poor battery performance.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -