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:com.mytwitter.Network.NetworkHelper.java

public static boolean connectedToWiFiNetwork(ConnectivityManager connectivityManager) {
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (!isConnected)
        return false;

    return (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:com.mytwitter.Network.NetworkHelper.java

public static boolean connectedToMobileNetwork(ConnectivityManager connectivityManager) {
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (!isConnected)
        return false;

    return (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE);
}

From source file:org.meruvian.midas.core.util.ConnectionUtil.java

public static boolean isInternetAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null)
        return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    else//from  w  w  w.j  av  a 2 s . co m
        return false;
}

From source file:com.photon.phresco.nativeapp.eshop.net.NetworkManager.java

public static boolean checkNetworkConnectivity(final Activity activity) {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else if (netInfo != null && (netInfo.getState() == NetworkInfo.State.DISCONNECTED
            || netInfo.getState() == NetworkInfo.State.DISCONNECTING
            || netInfo.getState() == NetworkInfo.State.SUSPENDED
            || netInfo.getState() == NetworkInfo.State.UNKNOWN)) {
        return false;
    } else {/*from   ww  w. j  a  v  a  2  s .  c  om*/
        return false;
    }
}

From source file:com.saltedge.sdk.network.SERestClient.java

/**
 * Check if networks are available.// www  .  j  a va  2s .c  om
 *
 * @return true if networks are available, false if not
 */
private static boolean isNetworkUnavailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) SaltEdgeSDK.getInstance().getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetworkInfo == null || !activeNetworkInfo.isConnected());
}

From source file:net.frakbot.FWeather.util.WeatherHelper.java

/**
 * Checks if there is any network connection active (or activating).
 *
 * @param context The current {@link Context}.
 * @return Returns true if there is an active connection, false otherwise
 *///w  ww . j  a v  a2  s  .  c o  m
public static boolean checkNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

From source file:Main.java

@SuppressWarnings({ "deprecation" })
public static String getNetWorkType(Context context) {
    String mNetWorkType = null;//from   w  w  w.  j a  va2  s. c  om
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return "ConnectivityManager not found";
    }
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        String type = networkInfo.getTypeName();
        if (type.equalsIgnoreCase("WIFI")) {
            mNetWorkType = NETWORKTYPE_WIFI;
        } else if (type.equalsIgnoreCase("MOBILE")) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            if (TextUtils.isEmpty(proxyHost)) {
                mNetWorkType = mobileNetworkType(context);
            } else {
                mNetWorkType = NETWORKTYPE_WAP;
            }
        }
    } else {
        mNetWorkType = NETWORKTYPE_INVALID;
    }
    return mNetWorkType;
}

From source file:Main.java

public static String getNetworkTypeName(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo;/*from  w  w w  .  j  av a2  s  .c  om*/
    String type = NETWORK_TYPE_DISCONNECT;
    if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
        return type;
    }

    if (networkInfo.isConnected()) {
        String typeName = networkInfo.getTypeName();
        if ("WIFI".equalsIgnoreCase(typeName)) {
            type = NETWORK_TYPE_WIFI;
        } else if ("MOBILE".equalsIgnoreCase(typeName)) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            type = TextUtils.isEmpty(proxyHost)
                    ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G)
                    : NETWORK_TYPE_WAP;
        } else {
            type = NETWORK_TYPE_UNKNOWN;
        }
    }
    return type;
}