Display an alert when internet connection not available in android application Display an alert when internet connection not available in android application android android

Display an alert when internet connection not available in android application


public boolean isOnline() {    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();    if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){        Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();        return false;    }return true; }

And you must add premission for accessing network state and Internet:

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


public void onCreate(Bundle obj) {    super.onCreate(obj)    setContextView(layout);    if (isOnline()) {        //do whatever you want to do     } else {        try {            AlertDialog alertDialog = new AlertDialog.Builder(con).create();            alertDialog.setTitle("Info");            alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                    finish();                }            });            alertDialog.show();        } catch (Exception e) {            Log.d(Constants.TAG, "Show Dialog: " + e.getMessage());        }    }}


You can use these methods anywhere

public void checkNetworkConnection(){    AlertDialog.Builder builder =new AlertDialog.Builder(this);    builder.setTitle("No internet Connection");    builder.setMessage("Please turn on internet connection to continue");    builder.setNegativeButton("close", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            dialog.dismiss();        }    });    AlertDialog alertDialog = builder.create();    alertDialog.show();}public boolean isNetworkConnectionAvailable(){    ConnectivityManager cm =            (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();    boolean isConnected = activeNetwork != null &&            activeNetwork.isConnected();    if(isConnected) {        Log.d("Network", "Connected");        return true;        }    else{        checkNetworkConnection();        Log.d("Network","Not Connected");        return false;    }}

when you need to check connection available call isNetworkConnectionAvailable() method.If the network not available it will pop up your dialog box. If you need to check network in multiple screen add these methods to the super class and inherit that class to other class and call this method when need