Dismiss alert dialog automatically when internet connection is back

18 Views Asked by At

I used alert dialog for when internet connection is off then show a alert dialog. Now i want if connection is back then alert dialog gone automatically.

Here is my code

    ConnectivityManager connectivityManager = (ConnectivityManager)         
    getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnected() || !networkInfo.isAvailable()) {


        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.alart_dialog);
        dialog.setCancelable(false);
        dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().getAttributes().windowAnimations =
                android.R.style.Animation_Dialog;
        Button button = dialog.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recreate();
            }


        });
        dialog.show();
        
    }
0

There are 0 best solutions below