I am showing my alert dialog in a separate thread and its not working for me. initially when I click
register button for 3000ms I am showing a progress dialogue. and after that I want to show a alert box but its not working. How to solve this?
Thanks in advance...!
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Trying to Login");
showDialog(0);
t = new Thread() {
public void run() {
showDialog(0);
try {
Thread.sleep(3000);
removeDialog(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
try {
some data sending to server
Object responce=(Object)soapEnvelope.getResponse();
temp=responce.toString();
if(temp.equals("1")){
temp = "The result is 1";
}
System.out.println("The result is "+temp);
new Thread()
{
public void run()
{
try
{
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AlertDialog.Builder successfullyLogin = new Builder(Register.this);
successfullyLogin.setCancelable(false);
successfullyLogin.setMessage("Successfully Login !").show();
//Toast.makeText(getBaseContext(), "Toast text", Toast.LENGTH_LONG).show();
}
};
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0: {
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while connecting...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
}
return dialog;
}
return null;
}
Take a look at AsyncTask
From JavaDocs: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Replace all your threading with AsyncTask classes. They are designed specifically for this type of thing, and work perfectly with the UI for showing dialogs, and dismissing them in the UI thread while still doing background work where you need it.
In this way, you don't need the 3000ms timeout, it just dismisses when it returns. Of course, you could time how long the login takes and keep the dialog up until your 3000ms is up if you want to, but I wouldn't. Also, if you want to pause in Android use
SystemClock.sleep(3000);
instead of java's native thread sleep, as you don't need to try/catch the interrupt.An example that replaces your code (notice the complete lack of threads, try/catches etc that usually litter threading code):
If you want the login to interrupt the dialog, then add a custom
TimeoutException
to thedoLogin()
, catch it in thedoInBackground()
, and return the appropriate Integer response and handle it inonPostExecute()
.I think a dialog can only be shown from the main UI thread. So the idea would be to have a handler in your main thread and post a message on it from the new thread, which will launch the dialog.
Inside thread you should use Handler for showing the AlertDialog:
Use below in place of showdialog(0);