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:it.unicaradio.android.utils.NetworkUtils.java

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

    if (info == null) {
        return false;
    }//from w  ww . j av a 2  s.com

    return info.isConnected();
}

From source file:com.air.mobilebrowser.NetworkUtil.java

/**
 * Check the current status of internet connectivity.
 * This method iterates over the available network interfaces and
 * checks for an active connection./*  w  w  w  .  j a  v a2  s .  c  o  m*/
 * @return true if a connection was detected, false otherwise.
 */
public static boolean haveInternetConnection(Context context) {
    if (context != null) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiInfo = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wimax = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        NetworkInfo blue = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
        NetworkInfo ether = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

        boolean hasInternet = false;

        if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (wimax != null && wimax.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (blue != null && blue.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (ether != null && ether.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        }

        return hasInternet;
    }

    return false;
}

From source file:Main.java

public static boolean isConnectedWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = null;/*from   w w w. jav  a2 s. co m*/
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }

    return networkInfo == null ? false : networkInfo.isConnected();
}

From source file:com.frame.network.utils.NetworkUtil.java

@TargetApi(11)
public static boolean is4GNetwork(Context cxt) {
    boolean isOpened4G = false;
    TelephonyManager telephonyManager = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= 11) {
        isOpened4G = telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE;
    }//from   ww w. ja  v  a2s .  co  m
    boolean isMobile = false;
    ConnectivityManager cm = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    isMobile = info == null ? false : info.isConnected();
    return isOpened4G && isMobile;
}

From source file:fr.pingtimeout.ConnectionUtils.java

public static boolean isOnWifi(Context context) {
    Log.d(loggerName, "Checking if connection is Wifi");

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifiNetworkInfo.isConnected()) {
        Log.d(loggerName, "Wifi is connected");
        return true;
    } else {/* w  w  w .  j  av  a 2s  .c  om*/
        Log.d(loggerName, "Wifi is not connected");
        return false;
    }
}

From source file:org.fdroid.enigtext.mms.MmsSendHelper.java

public static boolean hasNecessaryApnDetails(Context context) {
    try {// www.ja  v a 2  s  .  c  om
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        String apn = connectivityManager.getNetworkInfo(MmscProcessor.TYPE_MOBILE_MMS).getExtraInfo();

        MmsCommunication.getMmsConnectionParameters(context, apn, true);
        return true;
    } catch (ApnUnavailableException e) {
        Log.w("MmsSendHelper", e);
        return false;
    }
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * WIFI??//from   w ww  . ja  v  a 2 s .co  m
 * @param context
 * @return
 */
public static boolean isWifiAwailable(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return false;
    }
    return info.isAvailable();
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * WIFI?//w ww . ja  v a  2  s .com
 * @param context
 * @return
 */
public static boolean isWifiConnected(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return false;
    }
    return info.isConnected();
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * ?//from  w  w  w.jav  a2  s.c  o m
 * @param context
 * @return
 */
public static int getNetworkType(Context context) {
    if (context == null) {
        return -1;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return -1;
    }
    return info.getType();
}

From source file:org.thoughtcrime.securesms.mms.OutgoingMmsConnection.java

public static boolean isConnectionPossible(Context context) {
    try {/*  w  ww. j  a  v  a2s.c  o  m*/
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(MmsRadio.TYPE_MOBILE_MMS);
        if (networkInfo == null) {
            Log.w(TAG, "MMS network info was null, unsupported by this device");
            return false;
        }

        getApn(context, networkInfo.getExtraInfo());
        return true;
    } catch (ApnUnavailableException e) {
        Log.w(TAG, e);
        return false;
    }
}