java - Using Android ProgressDialog with AsyncTask and odd Activity Lifecycle -
i couldn't think of way form title make issue obvious, here goes:
i'm little on head diving asynctask first time. have app sends tweet. must kick out webview twitter authorization, comes onnewintent().
what i'm trying throw simple spinner progressdialog while it's connecting site/performing accesstoken work, , again while it's sending tweet. i've discovered need new thread progress bar. or rather, should doing "time-intensive work" in it's own separate thread make using progressdialog viable. question this: how can have progress spinner in foreground while authorization code works in background, , opens webview , comes back, , starts on @ onresume()?
i'm sure i'm not doing else in proper fashion. i'm new android, not java. i've put in create- , dismissdialog(int) calls should be, procedurally. as-is, otherwise works way should, dialogs not able show themselves.
i'm thinking should put entire authorize() , tweet() methods own asynctask. i'm not sure how go that, since authorize() (or more specifically, logintotwitter()) needs end saving data gets browser shared preferences after comes onnewintent().
thanks insights, == matt
public class integratetwitter extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override protected void onresume() { super.onresume(); mprefs = getsharedpreferences(prefs_name, mode_private); mtwitter = new twitterfactory().getinstance(); mtwitter.setoauthconsumer(consumer_key, consumer_secret); if(authorize()) { tweet(); returntomm(); } } private boolean authorize() { log.i(log_tag, "authorizing..."); showdialog(pd_authorizing); boolean result = false; if(responseexistsandvalid()) { saveresponsetoaccesstoken(); } if(isauthorized()) { log.i(log_tag, "prefs have accesstoken, grabbing it..."); if(getaccesstokenfromprefs()) { toast.maketext(integratetwitter.this, "authorized.", toast.length_short).show(); result = true; } } else { log.i(log_tag, "prefs don't have accesstoken."); if(!responsestringexists()) { log.i(log_tag, "no response exists either, starting twitter login process..."); toast.maketext(integratetwitter.this, "authorizing...", toast.length_short).show(); // here kicks out browser authentication logintotwitter(); } else { toast.maketext(integratetwitter.this, "authorization failed.", toast.length_short).show(); log.i(log_tag, "response exists, must have failed once already, skipping twitter login process."); returntomm(); } } deleteresponsefromprefs(); dismissdialog(pd_authorizing); return result; } private void tweet() { showdialog(pd_tweeting); try { date testdate = new date(); string testdatestring = dateformat.format("yyyy-mm-dd @ hh:mm:ss", testdate.gettime()).tostring(); mtwitter.updatestatus(testdatestring + " test tweet"); toast.maketext(this, "tweet successful!", toast.length_short).show(); } catch (twitterexception e) { toast.maketext(this, "tweet error.", toast.length_short).show(); log.i(log_tag, e.getmessage()); log.i(log_tag, arrays.tostring(e.getstacktrace())); } dismissdialog(pd_tweeting); } // bunch of support methods // ... }
try this.....
i think know if not using asynctask, can use thread along handler, post work done on non-ui thread ui thread.
asynctask provided android sync ui , non-ui work seamlessly.
i got example searching on google, but changed way wanted be.
here count till 50... , until keep displaying progressdialog. please see log while program executing see count increasing till 50.
public class asynctaskexampleactivity extends activity { protected textview _percentfield; protected button _cancelbutton; protected inittask _inittask; progressdialog pd; @override public void oncreate( bundle savedinstancestate ) { super.oncreate(savedinstancestate); setcontentview( r.layout.main ); _percentfield = ( textview ) findviewbyid( r.id.percent_field ); _cancelbutton = ( button ) findviewbyid( r.id.cancel_button ); _cancelbutton.setonclicklistener( new cancelbuttonlistener() ); _inittask = new inittask(); pd = progressdialog.show(asynctaskexampleactivity.this, "loading", "please wait"); _inittask.execute( ); } protected class cancelbuttonlistener implements view.onclicklistener { public void onclick(view v) { _inittask.cancel(true); } } /** * sub-class of asynctask */ protected class inittask extends asynctask<context, integer, string> { // -- run intensive processes here // -- notice datatype of first param in class definition matches param passed method // -- , datatype of last param in class definition matches return type of method @override protected string doinbackground( context... params ) { //-- on every iteration //-- runs while loop causes thread sleep 50 milliseconds //-- publishes progress - calls onprogressupdate handler defined below //-- , increments counter variable 1 int = 0; while( <= 50 ) { try{ thread.sleep( 50 ); publishprogress( ); i++; } catch( exception e ){ log.i("makemachine", e.getmessage() ); } } pd.dismiss(); return "complete!"; } // -- gets called before thread begins @override protected void onpreexecute() { log.i( "makemachine", "onpreexecute()" ); super.onpreexecute(); } // -- called publish progress // -- notice datatype of second param gets passed method @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); log.i( "makemachine", "onprogressupdate(): " + string.valueof( values[0] ) ); _percentfield.settext( ( values[0] * 2 ) + "%"); _percentfield.settextsize( values[0] ); } // -- called if cancel button pressed @override protected void oncancelled() { super.oncancelled(); log.i( "makemachine", "oncancelled()" ); _percentfield.settext( "cancelled!" ); _percentfield.settextcolor( 0xffff0000 ); } // -- called doinbackground method completes // -- notice third param gets passed method @override protected void onpostexecute( string result ) { super.onpostexecute(result); log.i( "makemachine", "onpostexecute(): " + result ); _percentfield.settext( result ); _percentfield.settextcolor( 0xff69adea ); _cancelbutton.setvisibility( view.invisible ); } } }
Comments
Post a Comment