Example usage for android.net NetworkInfo isAvailable

List of usage examples for android.net NetworkInfo isAvailable

Introduction

In this page you can find the example usage for android.net NetworkInfo isAvailable.

Prototype

@Deprecated
public boolean isAvailable() 

Source Link

Document

Indicates whether network connectivity is possible.

Usage

From source file:Main.java

public static boolean k(Context paramContext) {
    NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext.getSystemService("connectivity"))
            .getActiveNetworkInfo();//ww  w  . j a  v a  2s .  c  o  m
    if (localNetworkInfo != null) {
        return localNetworkInfo.isAvailable();
    }
    return false;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NETWORK_WIFI;/*from w  w w.j a va 2 s  . c om*/
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

            case NETWORK_TYPE_GSM:
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netType = NETWORK_2G;
                break;

            case NETWORK_TYPE_TD_SCDMA:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                netType = NETWORK_3G;
                break;

            case NETWORK_TYPE_IWLAN:
            case TelephonyManager.NETWORK_TYPE_LTE:
                netType = NETWORK_4G;
                break;
            default:

                String subtypeName = info.getSubtypeName();
                if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA")
                        || subtypeName.equalsIgnoreCase("CDMA2000")) {
                    netType = NETWORK_3G;
                } else {
                    netType = NETWORK_UNKNOWN;
                }
                break;
            }
        } else {
            netType = NETWORK_UNKNOWN;
        }
    }
    return netType;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }/*from w w w .  j  av  a2  s . com*/
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mWiFiNetworkInfo != null && mWiFiNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return mWiFiNetworkInfo.isAvailable();
        }/*from  w w w  . j av a  2 s . c  o m*/
    }
    return false;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isConnected() && mMobileNetworkInfo.isAvailable();
        }/*from  ww w  .  j a v  a2s .c  o m*/
    }
    return false;
}

From source file:Main.java

/**
 * Checks for internet access/*www.j  a  v  a2s  . c  o m*/
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasInternetAccess(@NonNull Context context) {
    try {
        boolean hasInternet = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if ((wifi.isAvailable() || mobile.isAvailable()) && networkInfo != null
                && networkInfo.isConnectedOrConnecting()) {

            hasInternet = true;
        }

        return hasInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * method to check for network availability. returns true for available and
 * false for unavailable//from w w w  .  j a v  a2 s .c o m
 */
public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiNetwork != null && wifiNetwork.isAvailable() == true
            && wifiNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else if (mobileNetwork != null && mobileNetwork.isAvailable() == true
            && mobileNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else
        return false;
}

From source file:mc.lib.network.NetworkHelper.java

public static boolean hasConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni != null && ni.isAvailable() && ni.isConnected()) {
        if (testConnection(TEST_SERVER1) || testConnection(TEST_SERVER2) || testConnection(TEST_SERVER3)) {
            Log.i(LOGTAG, "Internet connection detected");
            return true;
        }//from   w ww . ja va  2s .  c  o m
    }

    Log.i(LOGTAG, "No internet connection detected");
    return false;
}

From source file:Main.java

public static boolean checkInternet(Activity act) {
    ConnectivityManager conMgr = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo i = conMgr.getActiveNetworkInfo();
    if (i == null) {
        return false;
    }/* ww  w.  ja  v a2 s  .co  m*/
    if (!i.isConnected()) {
        return false;
    }
    if (!i.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mMobile = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean flag = false;
    if ((mWifi != null) && ((mWifi.isAvailable()) || (mMobile.isAvailable()))) {
        if ((mWifi.isConnected()) || (mMobile.isConnected())) {
            flag = true;//from www  .  j ava  2s . co  m
        }
    }
    return flag;
}