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

public static boolean isConnectedToWired(Context context) {
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //ConnectivityManager.TYPE_ETHERNET == 9 , but only from API 13

    NetworkInfo ethernet = connec.getNetworkInfo(9);

    if (ethernet != null && ethernet.isConnected()) {
        return true;
    } else {//w  w w  .  ja v  a2s. co m
        return false;
    }

}

From source file:Main.java

public static boolean isMobileDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isMobileDataEnable = false;
    isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();//from   w  w w . j ava  2 s  .  c  o  m
    return isMobileDataEnable;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }/*from   w w w .  jav  a 2 s. c  o  m*/
    }
    return false;
}

From source file:Main.java

/**
 * Get whether or not a wifi connection is currently connected.
 *//*w w w.ja  va  2s .  com*/
public static boolean isWifiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null)
        return false;
    return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
}

From source file:Main.java

/**
 * Check for wifi internet connectivity/*w w w . j  av  a2s  . c  om*/
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasWifiInternetAccess(@NonNull Context context) {
    try {
        boolean hasWifiInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi != null && wifi.isAvailable() && networkInfo != null
                && networkInfo.isConnectedOrConnecting()) {

            hasWifiInternet = true;
        }

        return hasWifiInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Check for 3g internet connectivity/*from   w  w w  . j  a  v a  2 s .co  m*/
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean has3gInternetAccess(@NonNull Context context) {
    try {
        boolean has3gInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobile != null && mobile.isAvailable() && // !ic_mobile.isRoaming() &&
                networkInfo != null && networkInfo.isConnectedOrConnecting()) {

            has3gInternet = true;
        }

        return has3gInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Checks for internet access// w ww .jav  a2 s.  c  om
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasInternetAccess(@NonNull Context context) {
    try {
        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:com.oneguy.recognize.Util.java

public static boolean isNetworkEnable(Context context) {
    ConnectivityManager con = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
    boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    boolean internet = con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    return wifi | internet;
}

From source file:Main.java

public static String getWifiName(Context context) {
    Log.v(TAG, "-- Calling WIFI NAME --");
    if (context != null) {
        ConnectivityManager conMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()) {
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
            Log.v(TAG, "-- WIFI is Enabled Name = " + wifiInfo.getSSID());
            return wifiInfo.getSSID();
        }/*from  w  w  w .j  av  a 2 s.  c o m*/
    }
    return "";
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static boolean isConnectedToWifi(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (info == null) {
        return false;
    }/*from ww  w  .  ja va 2s. co m*/

    return info.isConnected();
}