Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static boolean getNetWorkState(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }/*from  ww w.j  av  a 2  s. c  o  m*/

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isAvailable()) {
        return false;
    }

    return true;
}

From source file:Main.java

public static boolean checkConnection(Context context) throws NoConnectionPendingException {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity == null) {
        return false;
    } else {// www  .j  a  v  a2s  . com

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static int getNetType(Context context) {
    int netWorkType = NETWORK_CLASS_UNKNOWN;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        int type = networkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            netWorkType = NETWORK_WIFI;//w  w w.jav a  2  s .c  om
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            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:
                return NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }
        }
    }

    return netWorkType;
}

From source file:Main.java

public static boolean detect(Context context) {

    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        return false;
    }//from  w w w. ja  v  a 2  s . c  om
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();

    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }

    return true;
}

From source file:Main.java

public static String checkNetType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    String type = "no_network";
    if (info == null) {
        return type;
    }//from w  w w  .  j av a  2s  .  com
    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        type = "wifi";
    } else {
        switch (info.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            type = "UNKNOWN";
            break;
        case TelephonyManager.NETWORK_TYPE_GPRS:
            type = "GPRS";
            break;
        case TelephonyManager.NETWORK_TYPE_EDGE:
            type = "EDGE";
            break;
        case TelephonyManager.NETWORK_TYPE_UMTS:
            type = "UMTS";
            break;
        case TelephonyManager.NETWORK_TYPE_CDMA:
            type = "CDMA";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            type = "EVDO_0";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            type = "EVDO_A";
            break;
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            type = "1xRTT";
            break;
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            type = "HSDPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            type = "HSUPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPA:
            type = "HSPA";
            break;
        case TelephonyManager.NETWORK_TYPE_IDEN:
            type = "IDEN";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            type = "EVDO_B";
            break;
        case TelephonyManager.NETWORK_TYPE_LTE:
            type = "LTE";
            break;
        case TelephonyManager.NETWORK_TYPE_EHRPD:
            type = "EHRPD";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = "HSPAP";
            break;
        }
    }
    return type;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    int networkStatePermission = ctx.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

    if (networkStatePermission == PackageManager.PERMISSION_GRANTED) {

        ConnectivityManager mConnectivity = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        // Skip if no connection, or background data disabled
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }// www  . j  a  v  a 2  s .  c o  m
        // Only update if WiFi
        int netType = info.getType();
        // int netSubtype = info.getSubtype();
        if ((netType == ConnectivityManager.TYPE_WIFI) || (netType == ConnectivityManager.TYPE_MOBILE)) {
            return info.isConnected();
        } else {
            return false;
        }
    } else {
        return true;
    }
}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    if (context != null) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        try {/* w  w w.j av a 2 s  .co  m*/

            if (cm != null) {
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null) {

                    isNetworkAvailable = netInfo.isConnected();
                    //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                    //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
                }
            }
        } catch (Exception ex) {
            //Log.e("Network Avail Error", ex.getMessage());

        }
        //        check for wifi also
        if (!isNetworkAvailable) {

            WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            State wifi = cm.getNetworkInfo(1).getState();
            if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
                isNetworkAvailable = true;
                //Log.d("WIFI NETWORK<<","WIFI connected");
            } else {

                isNetworkAvailable = false;
                // Log.d("WIFI Network<<","WIFI not connected");
            }

        }
    }

    return isNetworkAvailable;

}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        return false;
    }/*from  w  ww  .java2 s  . c om*/

    NetworkInfo networkinfo = manager.getActiveNetworkInfo();

    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }

    return true;
}

From source file:Main.java

public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? -1 : networkInfo.getType();
}

From source file:Main.java

/**
 * Utility method to check Internet connection availability
 * /*from w ww .  j a v a  2  s  . com*/
 * @return boolean value indicating the presence of Internet connection
 *         availability
 */
public static boolean isNetworkAvailable(Activity argActivity) {
    if (argActivity == null) {
        return false;
    }

    ConnectivityManager connectivityManager;
    NetworkInfo activeNetworkInfo = null;
    try {
        connectivityManager = (ConnectivityManager) argActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
        activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return activeNetworkInfo != null;
}