我有一个警告对话框,显示何时没有互联网连接。我删除了' ok'按钮,以便用户无法解雇它。但是,如果互联网的状态已经改变,如何关闭它?!
这是我的警告对话框的代码:
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Showing Alert Message
alertDialog.show();
}
这就是我如何称呼它:
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
switch (v.getId()) {
case R.id.btn_login:
new AttemptLogin().execute();
default:
break;
}
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(Login.this, "No Internet Connection",
"You don't have internet connection.", false);
}
答案 0 :(得分:1)
尝试使用BroadcastReciver
收听ConnectivityManager.CONNECTIVITY_ACTION
。