Hi,
public class Connection extends Activity implements Runnable {
public static final int CONNECTION_ERROR = 1;
public static final int CONNECTION_DONE = 3;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
createConnection();
}
public void createConnection() {
m_ProgressDialog = ProgressDialog.show(this, "Please wait...","Connection ...", true, false);
thread = new Thread(this);
thread.start();
}
public void run() {
int i = connecTion();
handler.sendEmptyMessage(i);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == CONNECTION_ERROR) {
m_ProgressDialog.dismiss();
AlertDialog.Builder alt_bld = new AlertDialog.Builder(thisA);
alt_bld.setMessage("Failed to connect to the server");
alt_bld.setCancelable(false);
alt_bld.setNegativeButton("Quit",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {finish();}});
alt_bld.setPositiveButton("Try Again",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//HERE IS THE PROBLEM
/*m_ProgressDialog.show(thisA, "Please wait...", "Connection ...", true, false);
connecTion();*/
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("ChatApp");
alert.setIcon(R.drawable.icon);
alert.show();
}
else {
m_ProgressDialog.dismiss();
finish();
}
}
};
private int connecTion() {
/** Create a connection */
try {
//Function to create the connection (throwing error if there is a pb)
} catch (Exception e) {
Log.e("App","Failed to connect");
return CONNECTION_ERROR;
}
//If no error left, everything is OK
return CONNECTION_DONE;
}
I want to realize a "Try Again" button which launch again the thread to create the connection and the ProgressDialog in parallel. How can I kill the "old" thread and create the new one properly? Is it better to keep the same thread alive and just dealing with Handler and Messages? Use service?
Thank you !