Internet 签入对话框

Internet Check in dialog

我创建了一个项目,它使用广播接收器检查持续的互联网连接。 现在,如果互联网可用,它什么都不显示,但只要我通过滚动通知关闭我的互联网连接,它就会显示我的警告框,错误是没有互联网 connection.but 当我从滚动通知再次打开我的互联网时警告框保留在屏幕上如何关闭警告框。

我已经尝试全局删除我的 Alertd

//代码

 private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);



            if(currentNetworkInfo.isConnected()){

               // alertDialog.dismiss(); not working showing Error
//Attempt to invoke virtual method 'void androidx.appcompat.app.AlertDialog.dismiss()' on a null object reference
                // Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();



            }
               else{
 alerDialogbuilder = new AlertDialog.Builder(Registration.this);
            alerDialogbuilder.setTitle("Internet / Wifi Connection Error ! ");
            alerDialogbuilder.setMessage("Turn on Wifi/Internet ");

            alerDialogbuilder.setCancelable(false);
            alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), 0);

                }
            });
            alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent iteer = new Intent(Registration.this, MainActivity.class);
                    startActivity(iteer);
                    overridePendingTransition(R.anim.from_left, R.anim.slide_to_right);
                    finish();
                }
            });

            alertDialog = alerDialogbuilder.create();
                alertDialog.show();
            }
        }
    };

//简历调用

@Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
        this.registerReceiver(this.mConnReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

//内部创建

 this.registerReceiver(this.mConnReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

这样做

像这样在 onCreate() 中启动警报对话框,您使用现有代码一次又一次地创建警报对话框的新实例,这就是 dismiss() 不起作用的原因

onCreate(){
 alerDialogbuilder = new AlertDialog.Builder(Registration.this);
            alerDialogbuilder.setTitle("Internet / Wifi Connection Error ! ");
            alerDialogbuilder.setMessage("Turn on Wifi/Internet ");

            alerDialogbuilder.setCancelable(false);
            alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), 0);

                }
            });
            alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent iteer = new Intent(Registration.this, MainActivity.class);
                    startActivity(iteer);
                    overridePendingTransition(R.anim.from_left, R.anim.slide_to_right);
                    finish();
                }
            });

            alertDialog = alerDialogbuilder.create();

}

你的广播接收器看起来像这样

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);



            if(currentNetworkInfo.isConnected()){

              //dismiss dialog here
                alertDialog.dismiss();



            }
               else{
 //show dialog here
                alertDialog.show();
            }
        }
    };
//unregister broadtcast here
@Override
protected void onDestroy() {
    super.onDestroy();
    if (this.mConnReceiver != null) {
        unregisterReceiver(this.mConnReceiver);
    }


}

重要通知:-

unregister broadcast receiver in onStop() else there will be exception and crash

DO NO REGISTER RECEIVER AGAIN IN ONRESUME()