android - Executing two asynchronous tasks and updating AsyncTask message -
i have piece of code connects server , logs in. while these 2 tasks (connect server , login) beingness executed, progress dialog appears.
final progressdialog pd = new progressdialog(getactivity()); pd.setindeterminate(true); pd.setcancelable(false); // while connecting server, dialog should following: pd.settitle("connecting..."); pd.setmessage("connecting " + hostname + "..."); pd.show(); new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { seek { // first task executed. progress dialog should "connecting..." executetask1(); // when first task (connecting server) ready, want title // , message of progress dialog changed this: pd.settitle("logging in"); //// marked line, see below. //// pd.setmessage("trying log in server..."); // sec task executed. executetask2(); // shows screen , dismisses progressdialog. showscreen(screenconstant.list_residents); pd.dismiss(); } grab (exception exc) { } } }
but ran 2 problems:
aruntimeexception
raised when marked line (see above) executed: "only original thread created view hierarchy can touch views" the login task gets response server, response asynchronous. i want progressdialog
appear text "connecting..." while connection effort beingness executed. after connection successful, want text of progress dialog changed "logging in...", while login effort beingness performed.
now how should accomplish this?
editnotice executetask2()
triggers task run in different thread; sends login request server, hence need wait until thread signals me when it's ready.
any code touches ui should done on main thread, you'll need utilize onprogressupdate()
, publishprogress()
facilitate this.
eg:
public void onprogressupdate(void... progress){ super.onprogressupdate(progress); pd.settitle("logging in"); pd.setmessage("trying log in server..."): }
and in doinbackground()
method:
executetask1(); //task 1 finished, update ui publishprogress();
in add-on this, should dismiss progressdialog
in onpostexecute()
, executed on main thread
android android-asynctask progressdialog ui-thread
No comments:
Post a Comment