我想从我的 onPause() 方法中关闭我的警告框,因为我正在检查互联网连接,如果没有连接,我会显示一个警告框说没有连接。但是当我最小化应用程序并重新启动它时,前一个警报框在那里,所以总共有两个警报框,我想避免这种情况。所以如果我在 onPause() 方法中关闭它,第二个不会上来。
谁能帮我解决这个问题,谢谢。
if (!haveNetworkConnection) {
// Toast.makeText(getApplicationContext(), "No Connection",
// Toast.LENGTH_SHORT).show();
advtlayout.setVisibility(View.GONE);
System.out.println("no network");
showGraphOverlay();
hideOutputTextView();
hideProgressBar();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
InterPreterWithAnimation.this);
// Setting Dialog Title
alertDialog.setTitle("No Network");
// Setting Dialog Message
alertDialog.setMessage("Turn on Wifi or Mobile data");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to invoke YES event
InterPreterWithAnimation.this
.startActivity(new Intent(
Settings.ACTION_SETTINGS));
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
最佳答案
首先定义AlertDialog.Builder builder
全局并尝试检查 dialog
对象 dialog!=null
在onPause()
然后
if (dialog.isShowing()) {
dialog.dismiss();
}
关于android - 如何关闭警报框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21569016/