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

/**
 * Check whether Internet connection is enabled on the device
 * /*from   w  w w.  j  a v a  2s  .  c o  m*/
 * @param context
 * @return
 */
public static final boolean checkNetwork(Context context) {
    if (context != null) {
        boolean result = true;
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
            result = false;
        }
        return result;
    } else {
        return false;
    }
}

From source file:net.kayateia.lifestream.Network.java

static public boolean IsActive(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null)
        return false;
    else/*from   w  ww .  ja v  a 2s  .  c  o  m*/
        return activeNetwork.isConnectedOrConnecting();
}

From source file:Main.java

private static NetworkInfo getActiveNetworkInfo(Context context) {
    if (context == null) {
        return null;
    }//w ww  .j a v  a2s. co  m
    ConnectivityManager connectiveManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectiveManager == null) {
        return null;
    }
    return connectiveManager.getActiveNetworkInfo();
}

From source file:Main.java

public static boolean isWifiEnabled(Context context) {
    ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return ((mgrConn.getActiveNetworkInfo() != null
            && mgrConn.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED)
            || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    // showLog("getNetWorkType");
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == networkInfo || !networkInfo.isAvailable()) {
        return 1;
    }//w  w w . j  a  va  2  s. c o m
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
        return 2;
    }
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
        return 3;
    }
    return 4;
}

From source file:Main.java

public static boolean isInternetConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;

    boolean is_connect = false;

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null) {
        if (info.isConnected())
            is_connect = true;//from   ww w . ja va 2s .  c o m
    }

    return is_connect;
}

From source file:Main.java

public static boolean isWiFiEnabled(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return ((cm != null && cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED)
            || tm != null && tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}

From source file:Main.java

public static Map<String, String> getNetworkInfo(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    Map<String, String> map = new HashMap();
    NetworkInfo info = cm.getActiveNetworkInfo();
    if ((info == null) || (!info.isConnectedOrConnecting()) || (withinInBlackList())) {
        map.put("access_subtype", "offline");
        map.put("access", "offline");
        map.put("carrier", "");
    } else {//from  w  ww.  j  a  v a  2  s . c om
        map.put("access_subtype", info.getSubtypeName());
        map.put("access", cleanNetworkTypeName(info.getTypeName()));
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String carrierName = manager.getNetworkOperatorName();
        map.put("carrier", carrierName);
    }
    return map;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    if (context == null) {
        return false;
    }/*  ww w .jav  a2  s .c o  m*/
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            return info.isAvailable();
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean has(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }//from  w ww  .  jav a2s  .  c o m
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    return (info != null) && info.isAvailable();
}