I haven't read your code-snippet, but here is how i would do it: When the user wants to login, do a showDialog(idDialogLogin), containing the username/password fields Login/Cancel buttons.
When Login is pressed, dismiss the dialog and start a progress-dialog and the actual logging in. Handle this through an AsyncTask: onPreExecute shows the progress dialog. The background task does the actual logging in (through http). onPostExecute either hides it (successful login) or just does a showDialog(idDialogLogin) again of the login-dialog on an unsuccessful login. When cancel is pressed, just dismiss the dialog. On Jan 25, 9:47 am, qmwestview <[email protected]> wrote: > Hi there, > > I am having a problem about repeating Login dialog (an AlertDialog) > and progress dialog, coordinating with http thread. I suppose > repetitive Login dialog (if fail, continue) handling should be common > and straightforward. I guess my approach must be wrong somewhere. I > already spent 2 days on this and am desperate. So please help. > > The usecase is like this: > > I have a main activity. > > User starts the app, the main activity starts. > Show a login dialog (generated by the main thread, i.e. from onCreate > ()). The main thread then starts a wait_thread, which will wait for > http to return data and check the data and decide what to do. > > After user input username/password and press login, a progress dialog > starts. > > The progress dialog starts an http_thread to talk to the server and > get replies. Once done, it will notify the waiting thread. > > If the user type in the right username password first time, the code > works fine. > > But it always fail for 2nd time Login, i.e. When first login fail > (wrong username/password), the wait_thread will generate 2nd Login > dialog to let user repeat the login process. But after user hit the > login on this 2nd Login dialog, the system always crashes. > > The exception here is: > > ERROR/AndroidRuntime(21880): Uncaught handler: thread main exiting due > to uncaught exception > ERROR/AndroidRuntime(21880): android.view.ViewRoot > $CalledFromWrongThreadException: Only the original thread that created > a view hierarchy can touch its views. > > The followings are part of the code. > ... > public class Test extends Activity implements Constants,Runnable{ > ... > > @Override > public void onCreate(Bundle savedInstanceState) { > ... > showDialog(DIALOG_LOGIN); // > startHttpWaitThread(); > } > > private void startHttpWaitThread(){ > try { > Thread dialogThread = new Thread(this); > dialogThread.start(); > } catch(Exception e){ > //showDialog(DIALOG_LOGIN); > } > } > /* �...@override > public void onStart() { > super.onStart(); > } */ > > public void run(){ > Looper.prepare(); > > try { > synchronized(httpReplyData){ // httpReplyData holds all needed > data returned from server > httpReplyData.wait(); > } > } catch(Exception e) { > e.printStackTrace(); > } > if > (httpReplyData.getHttpResult()!=HTTP_REPLY_STATUS_CODE_OK_200) > showDialog(DIALOG_LOGIN); > Looper.loop(); > } > > @Override > protected Dialog onCreateDialog(int id) { > switch (id) { > case DIALOG_LOGIN: > return getLoginDialog(this);//, > case DIALOG_LOGIN_PROGRESS: > return new LoginProgressDialog > (ctxForLoginProgressDialog);// } > return null; > } > > private Dialog getLoginDialog(Context ctx) {//, String name, > String pwd){ > ... > dialogBuilder.setPositiveButton(R.string.login, new > DialogInterface.OnClickListener() { > public void onClick(DialogInterface dialog, int > whichButton) { > networkService.startHttpThread > (HTTP_REQUEST_LOGIN_INDEX, getUsername(), getPassword()); // > networkService is the HTTP manager > showDialog(DIALOG_LOGIN_PROGRESS); // > startHttpWaitThread(); > } > }); > return dialogBuilder.create(); > } > > private class LoginProgressDialog extends ProgressDialog > implements Constants{ > //private ProgressThread progressThread; > public LoginProgressDialog(Context ctx){ > super(ctx); > ... > } > } > > } > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~ > I also tried second approach, which moves the wait_thread into > progress dialog class/object, following Google’s “Creating a > ProgressDialog” (http://developer.android.com/guide/topics/ui/ > dialogs.html#ProgressDialog). The 2nd login dialog is started by the > progress dialog. Its wait() method gets called ONLY once (that is > before 3nd showDialog(DIALOG_LOGIN) gets called). Seems to me the > unfinished progressDialog’s thread blocks new progressDialog to be > generated, I mean, when new showDialog(DIALOG_LOGIN_PROGRESS) called, > the system will continue to use last progressDialog which has already > responded to the first notify call and won’t respond to the notify > call any more. So although the http thread handles well and call > notify in due time, no one is waiting for that notify. Thus the system > just hungs, while progress dialog is doing it animation endlessly. > > The followings are part of the code: > … > public class Test {// extends Activity implements Constants { > ... > @Override > public void onCreate(Bundle savedInstanceState) { > ... > showDialog(DIALOG_LOGIN); // > } > > ... > @Override > protected Dialog onCreateDialog(int id) { > switch (id) { > case DIALOG_LOGIN: > return getLoginDialog(this);// > case DIALOG_LOGIN_PROGRESS: > return new LoginProgressDialog(ctxForLoginProgressDialog);// > } > return null; > } > private Dialog getLoginDialog(Context ctx) {//, String name, > String pwd){ > ... > dialogBuilder.setPositiveButton(R.string.login, new > DialogInterface.OnClickListener() { > public void onClick(DialogInterface dialog, int > whichButton) { > ... > > networkService.startHttpThread(HTTP_REQUEST_LOGIN_INDEX, > username, password); > showDialog(DIALOG_LOGIN_PROGRESS); // > } > }); > return dialogBuilder.create(); > } > > private class LoginProgressDialog extends ProgressDialog > implements Constants{ > private ProgressThread progressThread; > public LoginProgressDialog(Context ctx){ > ... > progressThread = new ProgressThread(handler); > progressThread.start(); > } > final Handler handler = new Handler() { > public void handleMessage(Message msg) { > int httpStatusCode = > msg.getData().getInt("httpStatusCode"); > > if (httpStatusCode == HTTP_REPLY_STATUS_CODE_OK_200){ > dismissDialog > (DIALOG_LOGIN_PROGRESS); > } else showDialog(DIALOG_LOGIN); > } > }; > private class ProgressThread extends Thread { > Handler handler; > ProgressThread(Handler handler){ > this.handler = handler; > } > public void run() { > try { > synchronized(httpReplyData){ > httpReplyData.wait(); > } > } catch(Exception e) { > e.printStackTrace(); > } > Message msg = handler.obtainMessage(); > Bundle b = new Bundle(); > b.putInt("httpStatusCode", httpReplyData.getHttpResult > ()); > msg.setData(b); > handler.sendMessage(msg); > } > } > }} > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > How should I correct the logic? Any help would be truly appreciated -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

