android - UI thread waits for Async result and act according to result -


in android project trying call web service returns string result. after execution, result updated in ui thread using onpostexecution() , result decide whether move next activity or notify user correct information.

above intention achieve below code :

 backgroundtask bcktask = new backgroundtask();         bcktask.execute(servicemethodname, urlparameter, bmap);     thread.sleep(1000);     //do nothing     while (backgroundresult == null)         ;      if (backgroundresult == "sucessfully registered") {         intent intent = new intent(this, verifydetail.class);         startactivity(intent);     } else {         toast.maketext(this, backgroundresult, toast.length_long)                 .show();     } 

but problem when try run above code stucks @ ui thread , background thread not running or perhaps not getting cpu time execute. please tell me how can done. need hold activity , show message (what wrong) if result not sucessfully registered or screen change next activity.

thanks & regards,
sourabh

you can put callback in constructor of task achieve this.

pseudo code:

create interface following

public interface mycallback {     public void onresult(string result); } 

implement interface somewhere in code:

mycallback callback = new mycallback() {     @override     public void onresult(string result) {         if(result.equals("sucessfully registered") {             // success         } else {             // not success         }     } } 

pass callback in constructor of task , store local variable mcallback;

new backgroundtask(callback).execute(...); 

in onpostexecute() method:

mcallback.onresult("<web service result string>"); 

please note above has not been tested , might contain minor syntax errors. luck.


Comments