This problem drives me crazy. I miss some basic but very important knowledge about how to handle long operations in a new thread created within an activity and how to modify
Your Handler needs to be created in your UI thread for it to be able to update the UI.
I would then use the sendMessage method of the handler, rather than posting a runnable:
private static final int HANDLER_MESSAGE_ERROR = 0;
private static final int HANDLER_MESSAGE_COMPLETED = 1;
...
private void connectAndGetRoute(){
new Thread(){
@Override
public void run() {
try {
if(!connectTo9292ov()) return;
} catch(UnknownHostException e){
sendMessage(HANDLER_MESSAGE_ERROR);
} catch (ClientProtocolException e) {
sendMessage(HANDLER_MESSAGE_ERROR);
} catch (IOException e) {
sendMessage(HANDLER_MESSAGE_ERROR);
} finally {
sendMessage(HANDLER_MESSAGE_COMPLETED);
}
}
private void sendMessage(int what){
Message msg = Message.obtain();
msg.what = what;
mHandler.sendMessage(msg);
}
}.start();
}
...
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case HANDLER_MESSAGE_ERROR:
Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
break;
case HANDLER_MESSAGE_COMPLETED:
mProgressDialog.dismiss();
showOnScreen();
break;
default:
Log.w("MyTag","Warning: message type \""+msg.what+"\" not supported");
}
}
}