android - Run method from extends Activity extends Runnable -


i'm trying call method inside runnable running. waits string entered , when depending on string (the strings act commands) calls method , supposed run whats inside it.

public class app extends activity implements runnable {      public void run() {           try {              serversocket serversocket = new serversocket(portnum);              while (true) {                            socket client = serversocket.accept();                   try {                        bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream()));                       string str = in.readline();                       if(str.equals("test")) {                           //method call here                       }                   } catch(exception e) {                         log.d("app", e.getmessage());                   } {                             client.close();                         log.d("app", "done.");                     }              }                 } catch (exception e) {              log.d("app", e.getmessage());          }     } 

you should create handler object. each handler instance associated thread, , depending on thread want associate/handle messages on, should create handler within context. can push messages onto message queue of thread creating handler. string comes in can use this:

public handler handler_use = new handler(){      // @override     public void handlemessage(message msg) {        //do message     } } 

and inside try block:

try {     bufferedreader in = new bufferedreader(new  inputstreamreader(client.getinputstream()));     string str = in.readline();     if(str.equals("test")) {         //construct message msg - based on string         handler_use.sendmessage(msg);     } } 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -