java - Set boolean from outside Thread, inside the Thread-run -


i've got boolean outside thread. use method requires thread around returns boolean. how can set boolean outside thread returned boolean?

code:

// handle automatic e-mail sending in new thread new thread(new runnable() {     @override     public void run() {         // since want display toast within different thread,         // need use runonuithread display         runonuithread(new runnable() {             @override             public void run() {                 toast.maketext(checklistactivity.cactivity, d.t_sending_email, toast.length_long).show();             }         });         /*todo: emailsuccessfullysent = */emailsender.sendemail();     } }).start(); 

i've read somewhere final array so:

final boolean[] array = new boolean[1]; // handle automatic e-mail sending in new thread new thread(new runnable() {     @override     public void run() {         // since want display toast within different thread,         // need use runonuithread display         runonuithread(new runnable() {             @override             public void run() {                 toast.maketext(checklistactivity.cactivity, d.t_sending_email, toast.length_long).show();             }         });         array[0] = emailsender.sendemail();     } }).start(); emailsuccessfullysent = array[0]; 

but find rather odd. accepted way set value within thread, or there better way this?

it requires little more overhead, best way approach problem in android use handler. handler structure can use receive messages multiple threads , execute code on thread handler defined in. overriding handlemessage(message msg) method , defining few constants, can send messages thread , have them handled in ui thread.

code:

public boolean variable = false; private class myhandler extends handler {     public static final int message_toast = 1;     public static final int thread_return = 2;      public void handlemessage(message msg){         if(msg.what == message_toast){             toast.maketext(msg.getdata().getstring("toast"), toast.length_long).show();         } else if(msg.what == thread_return){             variable = msg.getdata().getboolean("returnvalue");         }     } } public myhandler handler = new myhandler(); 

this make thread this:

// handle automatic e-mail sending in new thread new thread(new runnable() {     @override     public void run() {         bundle bundle = new bundle();         bundle.setstring("toast","i want display message");         message msg = handler.obtainmessage(myhandler.message_toast);         msg.setdata(bundle);         msg.sendtotarget();         bundle.setboolean("returnvalue", emailsender.sendemail());         msg = handler.obtainmessage(myhandler.thread_return);         msg.setdata(bundle);         msg.sendtotarget();    } }).start(); 

it's little confusing when start using handlers, once know them, easy , powerful.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -