I'm trying to use handler in the purpose of waiting for my wifi connection. This the piece of code I'm using :
final AlertDialog alertDialog2 = new AlertDialog.Builder(new android.view.ContextThemeWrapper(context, R.style.AlertDialogCustom)).create();
alertDialog2.setTitle("Loading...");
alertDialog2.setIcon(R.drawable.check);
alertDialog2.show();
Handler handler = new Handler();
int count = 0;
while (!isConnected() /*Check wifi connection*/) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
alertDialog2.dismiss();
// do other thing
}
}, 200);
count++;
/*stop the loop after 20s*/
if (count > 100) {
break;
}
}
As you can see in the piece of code, I want to show a loading alertDialog during the operation and when it's finished I'd like to stop it to notify the user for his wifi connection.