Check Internet Connection - Is Device connected to Internet?

Use this method to check whether device is connected to internet or not? We use it in kind of check when we need to perform and internet base task in application. Like while Login we need to check if device is connect to Internet, if yes than we perform Login process otherwise application will show Internet Connection error message to user.

public boolean isNetworkConnected() {
    boolean check = false;
    try {
        ConnectivityManager mgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = mgr.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            check = true;
        } else {
            netInfo = mgr.getNetworkInfo(1);
            if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
                check = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return check;
}

Comments