android - AsyncTask.get() no progress bar -
my app sends data server. works fine until user in bad signal area. if user in signal area the following code works fine , data sent.
string[] params = new string[]{compid, tagid, tagclientid, carerid, formattedtagscantime, formattednowtime, statusforwbservice, getdevicename(), taglatitude, taglongitude}; asyncpostdata apd = new asyncpostdata(); apd.execute(params); .
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 .
the app in bad signal areas tends show progress bar few minutes before showing black screen while rendering app unusable.
i thought way around following.
string[] params = new string[]{compid, tagid, tagclientid, carerid, formattedtagscantime, formattednowtime, statusforwbservice, getdevicename(), taglatitude, taglongitude}; asyncpostdata apd = new asyncpostdata(); try { apd.execute(params).get(10, timeunit.seconds); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (timeoutexception e) { // todo auto-generated catch block e.printstacktrace(); } this cause asynctask cancel after 10 seconds, executing there black screen until data sent followed progressbar few millisecs.
is there way show progressbar whilst executing asynctask.get()?
thanks in advance. matt.
also there ideas why black screen comes when user in bad signal area , therefor no response server. senario seems cause app alot of problems it's behavior unusual afterwards sending transactions @ later date.
[edit1]
public class signalservice extends service{ nfcscannerapplication nfcscannerapplication; telephonymanager signalmanager; phonestatelistener signallistener; private static final int listen_none = 0; private static final string tag = signalservice.class.getsimplename(); @override public void oncreate() { super.oncreate(); // todo auto-generated method stub log.e(tag, "signalservice created"); nfcscannerapplication = (nfcscannerapplication) getapplication(); signallistener = new phonestatelistener() { public void onsignalstrengthchanged(int asu) { //log.e("onsignalstrengthchanged: " , "signal strength = "+ asu); nfcscannerapplication.setsignalstrength(asu); } }; } @override public void ondestroy() { super.ondestroy(); // todo auto-generated method stub log.e(tag, "signalservice destroyed"); signalmanager.listen(signallistener, listen_none); } @override public void onstart(intent intent, int startid) { super.onstart(intent, startid); // todo auto-generated method stub log.e(tag, "signalservice in onstart"); signalmanager = (telephonymanager) getsystemservice(context.telephony_service); signalmanager.listen(signallistener, phonestatelistener.listen_signal_strength); } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } }
you not need timer @ you're attempting (for reason thought trying loop asynctask based on comments above resulted in mine.). if understand correctly you're issue loss of service. have asynctask start may or may not finish depending on conditions. approach use , cancle task after fixed time in event did not finish executing before - assumption being if task didn't finish within 10 second cut off, service lost.
a better way approach problem use boolean flag indcates whether network connectivity available , stop task executing if service lost. here example took this post (i apologize formatting i'm on crappy computer - of things - ie8 - can't see code looks like).
public class mytask extends asynctask<void, void, void> { private volatile boolean running = true; private final progressdialog progressdialog; public mytask(context ctx) { progressdialog = gimmeone(ctx); progressdialog.setcancelable(true); progressdialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface dialog) { // set running = false; right here, i'll // stick contract. cancel(true); } }); } @override protected void onpreexecute() { progressdialog.show(); } @override protected void oncancelled() { running = false; } @override protected void doinbackground(void... params) { while (running) { // hard work } return null; } // ... } this example uses progress dialog allows user cancle task pressing button. you're not going rather you're going check network connectivty , set running boolean based on whether task connected internet. if connection lost - running bet set false trip while loop , stop task.
as work after task complete. should never use get. either (1) put needs done after doinbackgroundcompletes in onpostexecute (assuming not much) or (2) if need data starting activity use interface. can add interface either adding argument tasks constructor or using seperate method sets interface up. example
public void setinterface(ontaskcomplete listener){ this.listener = listener; } where ontaskcomplete listener declared instance variable in asynctask. note approach describing requires using seperate asynctask class. your's private right means need change project little.
update
to check connectivity use this.
public boolean isnetworkonline() { boolean status=false; try{ connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getnetworkinfo(0); if (netinfo != null && netinfo.getstate()==networkinfo.state.connected) { status= true; }else { netinfo = cm.getnetworkinfo(1); if(netinfo!=null && netinfo.getstate()==networkinfo.state.connected) status= true; } }catch(exception e){ e.printstacktrace(); return false; } return status; } you can check see if there actual network connection on app can connect ther server. method doesn't have public , can part of you're asynctask class. personally, use similar in network manager class use check various network statistics (one of can connect internet).
you check connectivity before started executing loop in doinbackground method , periodicly update throughout course of method. if netowkr available task continue. if not stop.
calling asynctask built in cancle method not sufficient becuase prevent onpostexecute running. not stop code execting.
Comments
Post a Comment