Set timeout function with AsyncTask [Android] -


in case, button pressed , process timeout. when button clicked verify accno web services, if verification (progressdialog) on 5 seconds stop , display alertdialog notice user "timeout".

but have not idea when in testing 1 milliseconds, in logically pause in alertdialog until pressed, display dialog in milliseconds auto dismiss , intent next activity. here code:

@override protected void oncreate(final bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_information3);  btn_next.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             if (title.gettext().tostring().equals("info")) {                 infoasynctask infoasynctask = new infoasynctask();                 try {                     infoasynctask.execute().get(1, timeunit.milliseconds);                 } catch (interruptedexception e) {                     e.printstacktrace();                 } catch (executionexception e) {                     e.printstacktrace();                 } catch (timeoutexception e) {                     alertdialog.builder alertdialog = new alertdialog.builder(information3.this);                     alertdialog.settitle(":: error ::");                     alertdialog.setmessage("verify timeout. please try again.");                      alertdialog.setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                         @override                         public void onclick(dialoginterface dialog, int which) {                             dialog.dismiss();                         }                     });                     alertdialog.show();                 }            }        });    }  private class infoasynctask extends asynctask<void, void, string> {     private string results;     private progressdialog pdialog;     private object resultrequestsoap;     string accno = et_accno.gettext().tostring().trim();     int timeout = 15000;      @override     protected void onpreexecute() {         pdialog = new progressdialog(information3.this);         pdialog.setmessage("verifying...");         pdialog.setindeterminate(false);         pdialog.setcancelable(true);         pdialog.show();         super.onpreexecute();     }      @override     protected string doinbackground(void... params) {         soapobject request = new soapobject(namespace, method_name);         request.addproperty("code", infocode);         request.addproperty("accno", accno);          soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11);         envelope.dotnet = true;         envelope.setoutputsoapobject(request);         httptransportse androidhttptransport = new httptransportse(url, timeout);         androidhttptransport.debug = true;          try {             androidhttptransport.call(soap_action, envelope);             string requestdumpstring = androidhttptransport.requestdump;             log.d("request dump: ", requestdumpstring);         } catch (exception e) {             e.printstacktrace();         }         try {             resultrequestsoap = envelope.getresponse();             results = resultrequestsoap.tostring();             log.d("output: ", results);         } catch (exception e) {             e.printstacktrace();         }         return null;     }      protected void onpostexecute(string result) {         if (resultrequestsoap == null) {             if (pdialog.isshowing()) {                 pdialog.dismiss();             } else {                 alertdialog.builder alertdialog = new alertdialog.builder(information3.this);                  alertdialog.settitle(":: error ::");                 alertdialog.setmessage("connection error, please check internet connection.");                  alertdialog.setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialog, int which) {                         dialog.dismiss();                     }                 });                 alertdialog.show();                 pdialog.dismiss();             }         } else {             if (results.equals("false")) {                 alertdialog.builder alertdialog = new alertdialog.builder(information3.this);                  alertdialog.settitle(":: warning ::");                 alertdialog.setmessage("please fill in correct account number");                  alertdialog.setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialog, int which) {                         dialog.dismiss();                     }                 });                 alertdialog.show();             } else if (et_billno.gettext().tostring().trim().isempty()) {                 alertdialog.builder alertdialog = new alertdialog.builder(information3.this);                  alertdialog.settitle(":: warning ::");                 alertdialog.setmessage("please fill in bill number");                  alertdialog.setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialog, int which) {                         dialog.dismiss();                     }                 });                 alertdialog.show();             } else if (et_amountno.gettext().tostring().trim().isempty() || integer.parseint(et_amountno.gettext().tostring().trim()) <= 0) {                 alertdialog.builder alertdialog = new alertdialog.builder(information3.this);                  alertdialog.settitle(":: warning ::");                 alertdialog.setmessage("please fill in amount");                  alertdialog.setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialog, int which) {                         dialog.dismiss();                     }                 });                 alertdialog.show();             } else if (results.equals("true")) {                 title = title.gettext().tostring();                 string value1 = et_accno.gettext().tostring().trim();                 string value2 = et_billno.gettext().tostring().trim();                 int value3 = integer.parseint(et_amountno.gettext().tostring().trim());                  addsearchinput(value1);                 addsearchinput(value2);                 addsearchinput(string.valueof(value3));                  intent intent = new intent(information3.this, confirmation3.class);                 intent.putextra("name", title);                 intent.putextra("value1", value1);                 intent.putextra("value2", value2);                 intent.putextra("value3", value3);                 startactivity(intent);             } else {                 super.onpreexecute();             }             pdialog.dismiss();         }     } } 

get() method block ui thread , guess don't want this.

you should implement cancel mechanics in doinbackground , call asynctask.cancel() timeout [like that](https://developer.android.com/reference/android/os/handler.html#postdelayed(java.lang.runnable, long))

final handler handler = new handler();     handler.postdelayed(new runnable() {       @override       public void run() {         asynctask.cancel();       }     }, 1); 

be aware there many gotchas asynctask, check article.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -