Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android: showing an alertdialog while internet is not connected in device

I want to check if the internet is connected and show a dialog if it is not. This is my code:

network_class.java

 public class network_class {
    public static boolean isConnectedToInternet(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
            else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }
        }
        return false;
    }
}

network_detector.java

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class network_detector extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        View alertdialog_1 = LayoutInflater.from(context).inflate(R.layout.network_dialog,null);
        AlertDialog.Builder dialog_1 = new AlertDialog.Builder(context);
        dialog_1.setView(alertdialog_1);

        AlertDialog dialog = dialog_1.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setCancelable(false);
        if(network_class.isConnectedToInternet(context)){
            if(dialog.isShowing())
                dialog.cancel();
        }
        else
            dialog.show();
    }
}

After that, I am using the receiver in my MainActivity:

network_detector detector = new network_detector();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //some irrelevant code here
}

@Override
protected void onStart() {
    IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(detector,intentFilter);
    super.onStart();
}

@Override
protected void onStop() {
    unregisterReceiver(detector);
    super.onStop();
}

While the application is running in my device, when I switch off the internet the dialog shows (which is correct), and then again when I switch on the internet, strangely enough the dialog still keeps on showing. Why is that? Where am I going wrong?

EDIT: These permissions are there in my manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

>Solution :

It doesn’t dismiss because you are creating a different AlertDialog every time you get on the onReceive so you should store a variable in the NetworkDetector class.

public class network_detector extends BroadcastReceiver {

    private AlertDialog dialog;

    @Override
    public void onReceive(Context context, Intent intent) {
         if (dialog != null) {
            boolean isConnected = networkClass.isConnectedToInternet(context);
            if (isConnected) {
                dialog.dismiss();
                dialog = null;
            } else if (!dialog.isShowing()) {
                showNoInternetDialog(context);
            }
        } else {
            showNoInternetDialog(context);
        }

}


    private void showNoInternetDialog(Context context) {
        View alertdialogView = LayoutInflater.from(context).inflate(R.layout.network_dialog, null);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setView(alertdialogView);

        dialog = dialogBuilder.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setCancelable(false);
        dialog.show();
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading