Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

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

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:Main.java

/**
 * Checks if the device is currently online, works for both wifi and mobile networks.
 *///from   w ww  .ja va  2  s  .  co  m
public static boolean isOnline(Context context) {
    if (context == null)
        return false;
    boolean state = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null)
        state = wifiNetwork.isConnectedOrConnecting();
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null)
        state = mobileNetwork.isConnectedOrConnecting();
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null)
        state = activeNetwork.isConnectedOrConnecting();
    return state;
}

From source file:Main.java

/**
 * Detects the availability of a working Wifi network connection to open a
 * http/https connection./*from  www. j  a  v  a 2 s. co  m*/
 *
 * @param context The context of the activity who is trying to check for the
 *                status of the network.
 * @return true if Wifi network is available, false otherwise.
 */
public static boolean isWiFiAvailable(Context context) {
    if (context == null)
        return false;

    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    } else {
        return false;
    }

}

From source file:Main.java

public static boolean hasConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }/*w  w w .  ja  va2s.c om*/

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }

    return false;
}

From source file:Main.java

public static boolean hasWifiConnection(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return info != null && info.isConnectedOrConnecting();
}

From source file:Main.java

public static int getMobileService(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info2 = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info2 == null || info2.getExtraInfo() == null) {
        return MOBILE_SERVICE_UNKNOW;
    }//ww w  .jav  a 2 s  .  c om
    String info = info2.getExtraInfo().toLowerCase();
    // //3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
    if ("3gwap".equals(info) || "3gnet".equals(info) || "uninet".equals(info) || "uniwap".equals(info)
            || "3gwap".equals(info)) {
        return MOBILE_SERVICE_UN;
    } else if ("cmnet".equals(info) || "cmwap".equals(info)) {
        return MOBILE_SERVICE_CM;
    } else if ("ctnet".equals(info) || "ctwap".equals(info)) {
        return MOBILE_SERVICE_CT;
    } else {
        return MOBILE_SERVICE_UNKNOW;
    }
}

From source file:Main.java

public static boolean hasConnection(Context c) {

    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }/*from ww w.ja v  a2  s.c  om*/

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }

    return false;

}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return wifiInfo.isConnected();
}

From source file:Main.java

public static boolean isWifiActive(Context c) { // Check if we are connected to the network via WiFi
    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi == null) {
        return false;
    }//from   w  ww.j av  a 2s.  c o  m
    if (wifi.isConnected()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static NetworkInfo getLanNetworkIfPossible(ConnectivityManager connMgr) {
    NetworkInfo result = null;//from  w  w w  .j av  a 2 s  .c om

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        result = connMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    }

    return result;
}

From source file:Main.java

private static boolean isMobileConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info != null && info.isConnected()) {
        return true;
    }/* w w w.j a  v  a 2  s .com*/

    return false;
}