android - Unable to cancel Async task on button click -


i trying cancel/end async task on button click of pop-up box. however, when click on button oncancelled() method not being called. please me this? below code:

public authenticationasync(context context, final string rid) {     this.rid = rid;      alertdialog = new alertdialog.builder(context).create();     alertdialog.settitle("information");     alertdialog.setmessage("raspberrypi validated");     alertdialog.setbutton(alertdialog.button_neutral, "ok",             new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int which) {                     cancel(true); //cancelling async task on button click                     dialog.dismiss();                 }             }); }   @override protected object doinbackground(object[] params) {     string metricdata = string.valueof(kurapayload.getmetric("data"));     if (metricdata.isempty())          subresponse = false; }  @override protected void oncancelled() {     log.d("async","cancelled");  //not being called }  @override protected void onpostexecute(object o) {     if (subresponse) {         asynchttpclient = new asynchttpclient();         asynchttpclient.get(wsconstants.add_rpi_ws + mqttfactory.getraspberrypibyid(), new asynchttpresponsehandler() {              @override             public void onsuccess(int statuscode, header[] headers, byte[] responsebody) {                 toast.maketext(activitycontexts.getmainactivitycontext(), "raspberrypi validated", toast.length_long).show();                 alertdialog.show();             }      }  } } 

it looks it's never getting oncancelled() because calling alertdialog.show() in onpostexecute(). since doinbackground() has completed, there nothing cancel.

the cancel() method meant called during execution of doinbackground() method, method runs on background thread.

note if cancel() called during execution of doinbackground(), execution of method won't stopped immediately. should periodic checks iscancelled() , if returns true, exit out of method.

after doinbackground() has completed, if cancel() has been called, oncancelled() called instead of onpostexecute().

here simple test based on code:

public class authenticationasync extends asynctask {      alertdialog alertdialog;     string rid;      public authenticationasync(context context, final string rid) {         this.rid = rid;          alertdialog = new alertdialog.builder(context).create();         alertdialog.settitle("information");         alertdialog.setmessage("raspberrypi validated");         alertdialog.setbutton(alertdialog.button_neutral, "ok",                 new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         cancel(true); //cancelling async task on button click                         dialog.dismiss();                     }                 });     }      @override     protected void onpreexecute(){         //for testing, showing dialog before background thread starts         alertdialog.show();     }      @override     protected object doinbackground(object[] params) {        //cycle forever until user clicks ok        while (true){            try {                thread.sleep(1000);            } catch (interruptedexception e) {                e.printstacktrace();            }            //check if user clicked ok in dialog            if (iscancelled()){                //exit method if user dismissed dialog                return null;            }        }     }      @override     protected void oncancelled() {         log.d("async", "cancelled");  //this called         toast.maketext(mainactivity.this, "cancelled", toast.length_long).show();     }      @override     protected void onpostexecute(object o) {         toast.maketext(mainactivity.this, "postexecute", toast.length_long).show();     } } 

result:

dialog shown during execution of doinbackground():

dialog shown

after clicking ok, oncancelled() called:

oncancelled


Comments

Popular posts from this blog

swift - How to change text of a button with a segmented controller? -

javascript - Fabric.js copy paste of selected group -