java - Threading and events -
i'm stuck here, i've read lot on threading on android i'm unable find answer suits proyect.
i've got frontend (manages gui) , backend (manages data , stuff). need update gui backend finishes running thread can't figure out how!
main.java
package frontend
public class main extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); thread thread = new thread() { @override public void run() { server server = new server(getapplicationcontext()); } }; thread.start();
server.java
package backend
public static list<string> lista = new arraylist<string>(); public server(context context) { revisar archivo = new revisar(); archivo.dosomething(); }
after archivo.dosomething
finishes need update gui backend data stored in static list.
any suggestions?
as you've surmised, can't update gui background thread.
typically, want, use message handling mechanism pass message gui thread. typically, pass runnable executed in gui thread. can pass message if you've subclassed handler , added code deal messages.
messages passed handlers. can either create own handler in gui thread, or use 1 of several exist. example, every view object includes handler.
or can use runonuithread() activity method.
pattern 1, handler plus runnables:
// main thread private handler handler = new handler(); ... // other thread handler.post(new runnable() { public void run() { log.d(tag, "this being run in main thread"); } });
pattern 2, handler plus messages:
// main thread private handler handler = new handler() { public void handlemessage(message msg) { log.d(tag, "dealing message: " + msg.what); } }; ... // other thread message msg = handler.obtainmessage(what); handler.sendmessage(msg);
pattern 3, call runonuithread():
// other thread runonuithread(new runnable() { // available in activity public void run() { // perform action in ui thread } });
pattern 4, pass runnable view's built-in handler:
// other thread myview.post(new runnable() { public void run() { // perform action in ui thread, presumably involving view } });
Comments
Post a Comment