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

/**
 * Checks device for network connectivity
 *
 * @return If the device has data connectivity
 *///from  w w  w .  ja  v  a  2s  . co m
public static boolean isNetworkAvailable(Context context) {
    boolean state = false;
    if (context != null) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            Log.i(TAG, "The device currently has data connectivity");
            state = true;
        } else {
            Log.i(TAG, "The device does not currently have data connectivity");
            state = false;
        }
    }
    return state;
}

From source file:Main.java

public static boolean hasInternetAccess(Context context) {
    try {/* ww w .j  a v  a  2  s  .c  o  m*/
        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

/**
 * Checks if Android is online//from  w  w w . j  a  va 2s  .  c o  m
 *
 * @param context
 * @return returns true if online
 */
public static boolean isAndroidOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo inetInfo = connectivityManager.getActiveNetworkInfo();
    return inetInfo != null && inetInfo.isConnected();
}

From source file:Main.java

private static boolean isCMWap(Context context) {
    boolean res = false;
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo == null) {
        return res;
    }/*from  ww  w.  ja  va 2 s.c o  m*/
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        if (networkInfo.getExtraInfo().toLowerCase().equals("cmwap")) {
            res = true;
        }
    }

    return res;
}

From source file:Main.java

/***
 * //from  www  . j  av  a2  s  .  c  o m
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
    final ConnectivityManager connectivityManager = ((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE));
    return connectivityManager.getActiveNetworkInfo() != null
            && connectivityManager.getActiveNetworkInfo().isConnected();
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String getAPN(Context context) {
    String apn = "";
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();

    if (info != null) {
        if (ConnectivityManager.TYPE_WIFI == info.getType()) {
            apn = info.getTypeName();// w  w  w  . j  av a  2s . c  om
            if (apn == null) {
                apn = "wifi";
            }
        } else {
            apn = info.getExtraInfo().toLowerCase();
            if (apn == null) {
                apn = "mobile";
            }
        }
    }
    return apn;
}

From source file:Main.java

public static boolean isMobile(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null) && (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE);
}

From source file:Main.java

public static boolean isNetDeviceAvailable(Context context) {
    boolean bisConnFlag = false;
    ConnectivityManager conManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = conManager.getActiveNetworkInfo();
    if (network != null) {
        bisConnFlag = conManager.getActiveNetworkInfo().isAvailable();
    }// w  w w  .ja v  a2  s. com
    return bisConnFlag;
}

From source file:Main.java

public static boolean checkNetWorkIsAvailable(Context context) {
    ConnectivityManager conManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = conManager.getActiveNetworkInfo();
    if (network != null) {
        return conManager.getActiveNetworkInfo().isAvailable();
    }/*from w w w . j  a  va 2  s  .c  o m*/
    return false;
}