android - 如何关闭警报框?

标签 android android-alertdialog onpause

我想从我的 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!=nullonPause()然后

if (dialog.isShowing()) {
 dialog.dismiss();
}

关于android - 如何关闭警报框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21569016/

相关文章:

android - 应用层 onPause

android - 无法完成 Activity

c# - 使用 c# 从 Azure 存储帐户表检索记录,同时使用 xamarin 开发跨平台应用程序给出错误

java - 重启 Android 后自定义小部件中的 Android 持久可检查菜单

java - 使用子字符串搜索过滤 ListView

android - 如何防止屏幕方向改变从相机 Intent 返回(SGS3)

java - 如何临时存储对象列表

android - TextToSpeech.setEngineByPackageName() 没有设置任何东西

Android:如何自定义 AlertDialog、Dialog 和 Spinner 标题

android - 如何将时间选择器对话框用于其他用途?