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:jfabrix101.lib.helper.NetworkHelper.java

/**
 * Check if the internet connection is through wifi.
 * // www .j a  va2  s.  com
 * @param context
 * @return true if the connection is through wifi
 */
public static boolean isWiFiConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        //mobile
        //State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

        //wifi
        State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

        //         if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
        //             //mobile
        //         } else if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
        //             //wifi
        //         }

        if (wifi == State.CONNECTED || wifi == State.CONNECTING)
            return true;
        else
            return false;

    } catch (Exception ex) {
        Logger mLogger = Logger.getInstance(NetworkHelper.class);
        mLogger.error("Exception in checkInternetConnection() : %s", ex.getMessage());
    }

    return false;
}

From source file:cm.aptoide.pt.util.NetworkUtils.java

public static boolean isPermittedConnectionAvailable(Context context, ViewIconDownloadPermissions permissions) {
    ConnectivityManager connectivityState = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean connectionAvailable = false;
    if (permissions.isWiFi()) {
        try {//  ww w .  ja v a2s  . co m
            connectionAvailable = connectionAvailable
                    || connectivityState.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED;
            if (ApplicationAptoide.DEBUG_MODE)
                Log.d("ManagerDownloads", "isPermittedConnectionAvailable wifi: " + connectionAvailable);
        } catch (Exception e) {
        }
    }
    if (permissions.isWiMax()) {
        try {
            connectionAvailable = connectionAvailable
                    || connectivityState.getNetworkInfo(6).getState() == NetworkInfo.State.CONNECTED;
            if (ApplicationAptoide.DEBUG_MODE)
                Log.d("ManagerDownloads", "isPermittedConnectionAvailable wimax: " + connectionAvailable);
        } catch (Exception e) {
        }
    }
    if (permissions.isMobile()) {
        try {
            connectionAvailable = connectionAvailable
                    || connectivityState.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED;
            if (ApplicationAptoide.DEBUG_MODE)
                Log.d("ManagerDownloads", "isPermittedConnectionAvailable mobile: " + connectionAvailable);
        } catch (Exception e) {
        }
    }
    if (permissions.isEthernet()) {
        try {
            connectionAvailable = connectionAvailable
                    || connectivityState.getNetworkInfo(9).getState() == NetworkInfo.State.CONNECTED;
            if (ApplicationAptoide.DEBUG_MODE)
                Log.d("ManagerDownloads", "isPermittedConnectionAvailable ethernet: " + connectionAvailable);
        } catch (Exception e) {
        }
    }

    if (ApplicationAptoide.DEBUG_MODE)
        Log.d("ManagerDownloads",
                "isPermittedConnectionAvailable: " + connectionAvailable + "  permissions: " + permissions);
    return connectionAvailable;
}

From source file:org.lol.reddit.common.General.java

public static boolean isConnectionWifi(final Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return info != null && info.isConnected();
}

From source file:Main.java

public static final boolean hasConnectivity(final Context context, final int... networkTypes) {
    final ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (networkTypes.length == 0)
        for (final NetworkInfo info : connectivity.getAllNetworkInfo())
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
    for (final int networkType : networkTypes)
        if (ConnectivityManager.isNetworkTypeValid(networkType)) {
            final NetworkInfo info = connectivity.getNetworkInfo(networkType);
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
        }//w  w  w. j  a  v  a 2 s  .  c  om
    return false;
}

From source file:avivi.com.pollardi.ImageGridFragment.java

public static boolean isInternetAvailable(Context context) {

    //        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    //        NetworkInfo netInfo = cm.getActiveNetworkInfo();
    //        return netInfo != null && netInfo.isConnectedOrConnecting();

    // get Connectivity Manager object to check connection
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);

    // Check for network connections
    if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED
            || connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING
            || connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING
            || connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {

        // if connected with internet

        Toast.makeText(context, " Connected ", Toast.LENGTH_LONG).show();
        return true;

    } else if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED
            || connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED) {

        Toast.makeText(context, " Not Connected ", Toast.LENGTH_LONG).show();
        return false;
    }/*from w w w. java2 s.c  om*/
    return false;

}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static boolean isConnectedToInternet(Context c) {
    ConnectivityManager connec = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);

    return (connec.getNetworkInfo(0).isConnectedOrConnecting()
            || connec.getNetworkInfo(1).isConnectedOrConnecting()) ? true : false;

}

From source file:org.openremote.android.console.net.ORNetworkCheck.java

/**
 * Detects the current WiFi status.//from ww w.j a va  2  s .  c  o  m
 *
 * @param ctx     global Android application context
 *
 * @return true if WiFi is available, false otherwise
 */
private static boolean canReachWifiNetwork(Context ctx) {

    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (!wifiManager.isWifiEnabled() || wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
        Log.i(LOG_CATEGORY, "WiFi not enabled or WiFi network not detected.");

        return false;
    }

    NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (!wifiNetworkInfo.isAvailable()) {
        Log.i(LOG_CATEGORY, "Wifi network detected but wasn't available.");

        return false;
    }

    else {
        return true;
    }
}

From source file:com.popcorntime.apps.remote.utils.Utils.java

public static boolean isWIFIConnected(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi != null && mWifi.isConnected())
        return true;

    return false;
}

From source file:com.theonespy.util.Util.java

public static boolean syncMethodOk() {
    if (Util.getSyncMethod().equals(Util.SYNC_METHOD_OFF)) {
        return false;
    } else if (Util.getSyncMethod().equals(Util.SYNC_METHOD_WIFI)) {
        ConnectivityManager connManager = (ConnectivityManager) MyApplication.getAppContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        return connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    } else {//w  w w . j a  va  2s .c  om
        return true;
    }
}

From source file:com.ppshein.sevendaynews.common.java

public static Boolean isNetAvailable(Context con) {
    try {/*  ww w . j a  va  2 s.  c o m*/
        ConnectivityManager connectivityManager = (ConnectivityManager) con
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}