java - android threading and handler - Changing UI objects from a background thread -


i've written small android application send , receive udp messages on local network.
i have udp receiver thread runs listen udp packets, want happen button become enabled on ui when packet received contains string of data. know has done handler, problem being have small amount of knowledge threads , little knowledge handlers.
able shed light on how handler put code?
thanks

code:

public void startudplistener() {      // creates listener thread     listen = true;     thread listenthread = new thread(new runnable() {          @override         public void run() {              try {                  log.i(log, "listener started!");                 datagramsocket socket = new datagramsocket(broadcastport);                 socket.setsotimeout(1500);                 byte[] buffer = new byte[buffersize];                 datagrampacket packet = new datagrampacket(buffer, buffersize);                   while(listen) {                      try {                          log.i(log, "listening packets");                         socket.receive(packet);                         string data = new string(buffer, 0, packet.getlength());                         log.i(log, "udp packet received "+ packet.getaddress() +" packet contents: " + data);                         }                     catch(ioexception e) {                          log.e(log, "ioexception in listener " + e);                     }                 }                  log.i(log, "listener ending");                 socket.disconnect();                 socket.close();                 return;             }              catch(socketexception e) {                  log.e(log, "socketexception in listener " + e);              }         }     });      listenthread.start();  } 

you add 1 , send either message or runnable.

private final handler muithreadhandler = new handler(looper.getmainlooper());  void fromanythread() {     final string importantdatafrombackgroundthread = "!!!";     muithreadhandler.post(new runnable() {         @override         public void run() {             system.out.println("hi ui thread:" + importantdatafrombackgroundthread);         }     }); } 

the handler handles messages / runnables within thread runs in. if specify looper.getmainlooper() guaranteed main thread.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -