android - cancelling AsyncTask and it's progressbar from a handler -
i want cancel asynctask sends data server after period of few seconds. i've got asynctask , handler set progress dialog still spins. how can cleanly stop asynctask quick possible. have far. in advance.
private class asyncpostdata extends asynctask<string, void, string> { progressdialog progressdialog; string datetimescanned; @override protected void onpreexecute() { // progressdialog= progressdialog.show(nfcscanneractivity.this, // "connecting server"," posting data...", true); int buildversionsdk = build.version.sdk_int; int buildversioncodes = build.version_codes.gingerbread; log.e(tag, "buildversionsdk = " + buildversionsdk + "buildversioncodes = " + buildversioncodes); int themeversion; if (build.version.sdk_int > build.version_codes.gingerbread) { themeversion = 2; }else{ themeversion = 1; } progressdialog = new progressdialog(nfcscanneractivity.this, themeversion); progressdialog.settitle("connecting server"); progressdialog.setmessage(" sending data server..."); progressdialog.setindeterminate(true); try{ progressdialog.show(); }catch(exception e){ //ignore } }; @override protected string doinbackground(string... params) { log.e(tag, "carerid in doinbackground = " + params[3] + " datetimescanned in asyncpost duplecate tx = " + params[4]); datetimescanned = params[4]; return nfcscannerapplication.loginwebservice.postdata(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7] + getversionname(), params[8], params[9]); } @override protected void onpostexecute(string result) { super.onpostexecute(result); try{ progressdialog.dismiss(); }catch(exception e){ //ignore } if( result != null && result.trim().equalsignorecase("ok") ){ log.e(tag, "about update db servertime"); datetime senttoserverat = new datetime(); nfcscannerapplication.loginvalidate.updatetransactionwithservertime(senttoserverat,null); nfcscannerapplication.loginvalidate.insertintoduplicatetransactions(datetimescanned); tagid = null; tagtype = null; tagclientid = null; //called refresh unsent transactions textview onresume(); }else if(result != null && result.trim().equalsignorecase("error: tx duplicated")){ log.e(tag, "response server duplicate transaction "); //nb. following time may not correspond time on server //because tx has been processed 'ok' never reached phone, //so going update phone's db duptx time phone doesn't keep //sending it. datetime senttoservertimewhenduptx = new datetime(); nfcscannerapplication.loginvalidate.updatetransactionwithservertime(senttoservertimewhenduptx,null); tagid = null; tagtype = null; tagclientid = null; }else{ toast.maketext(nfcscanneractivity.this, "no phone signal or server problem", toast.length_long).show(); } } }//end of asyncpostdata .
public void inithandlercancelasyncpost(){ cancelasyncposthandler = new handler(); cancelasyncpostrunnable = new runnable() { public void run() { cancelasyncpost(); } private void cancelasyncpost() { apd.cancel(true); } }; } .
public void sendtransactiontowebservice(string statusforwbservice){ inithandlercancelasyncpost(); cancelasyncposthandler.postdelayed(cancelasyncpostrunnable, 8 * 1000); string[] params = new string[]{compid, tagid, tagclientid, carerid, formattedtagscantime, formattednowtime, statusforwbservice, getdevicename(), taglatitude, taglongitude}; apd = new asyncpostdata(); apd.execute(params); }
Comments
Post a Comment