I have a scenario where my application tries to connect to the server, meanwhile a ProgressDialog is shown so the user knows that its contacting server. However, if the connection fails or refuses for some reason then I want to show another dialog saying that the connection refused by the server or something.
What I'm doing for this is, show the ProgressDialog then try to connect with the server, if it throws exception then I show AlertDialog with error message. But its not working, the code throwing exception when I call AlertDialog immediately after ProgressDialog. I also dismiss the ProgressDialog then start AlertDialog but doesn't work.
Do I need to change my approach or is there something wrong in my code? here is what I'm doing in the code
ProgressDialog pg = ProgressDialog.show(this, null, "Contacting server. Please wait...", false);
ConnectionThread ct = new ConnectionThread(web);
ct.setProgressDialog(pg);
ct.setErrorDialog(dialog);
ct.start();
This is the ConnectionThread class from which an attempt to connect to the server is made through webAccess class
public class ConnectionThread extends Thread {
private WebAccess _webAccess;
private ProgressDialog _progressDialog;
private AlertDialog _alertDialog;
public ConnectionThread(WebAccess webAccess) {
_webAccess = webAccess;
}
public void setProgressDialog(ProgressDialog dialog) {
_progressDialog = dialog;
}
public void setErrorDialog(AlertDialog dialog) {
_alertDialog = dialog;
}
@Override
public void run() {
try {
_webAccess.invoke();
_progressDialog.dismiss();
} catch (Exception ex) {
_progressDialog.cancel();
_alertDialog.show();
}
}
}