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:com.matze5800.paupdater.Functions.java

public static boolean WifiConnected(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi.isConnected();
}

From source file:Main.java

public static String getNetworkConnType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null == connectMgr) {
        return null;
    }/*from  w w w .  j av a  2 s .c  o m*/

    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return null;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return null;
    }

    NetworkInfo mobileNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (null == mobileNetworkInfo) {
        return null;
    }
    NetworkInfo.State mobile = mobileNetworkInfo.getState();
    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_MOBILE;
    }

    NetworkInfo wifiNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (null == wifiNetworkInfo) {
        return null;
    }
    NetworkInfo.State wifi = wifiNetworkInfo.getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_WIFI;
    }
    return null;
}

From source file:com.andrew.apollo.utils.ApolloUtils.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one/*  ww w .  java2 s .  c  om*/
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}

From source file:fr.mixit.android.utils.NetworkUtils.java

public static ConnectivityState getConnectivity(Context ctx) {
    ConnectivityState currentNetworkType;
    final ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        NetworkInfo networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
            currentNetworkType = ConnectivityState.WIFI;
        } else {//from  ww w .j av  a 2 s  . c  om
            networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                currentNetworkType = ConnectivityState.CARRIER;
            } else {
                // how can we be there ? no wifi and no mobile data but connected ?
                currentNetworkType = ConnectivityState.UNKNOWN;
            }
        }
    } else {
        currentNetworkType = ConnectivityState.NONE;
    }

    return currentNetworkType;
}

From source file:com.iStudy.Study.Renren.Util.java

/**
 * ??/*from  ww  w .  ja va  2 s  .co m*/
 * @param
 *       Context
 * @return 
 *       true - ?3G/GSM?wifi
 *       false - ?contextnull
 */
public static boolean isNetworkConnected(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State mobileState = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    State wifiState = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (mobileState == State.DISCONNECTED && wifiState == State.DISCONNECTED) {
        return false;
    }
    return true;
}

From source file:com.playhaven.android.req.PlayHavenRequest.java

protected static PlayHaven.ConnectionType getConnectionType(Context context) {
    try {//from  w w  w  .  j a  v  a  2s.c o  m
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (manager == null)
            return PlayHaven.ConnectionType.NO_NETWORK; // happens during tests

        NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiInfo != null) {
            NetworkInfo.State wifi = wifiInfo.getState();
            if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.WIFI;
        }

        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileInfo != null) {
            NetworkInfo.State mobile = mobileInfo.getState();
            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.MOBILE;
        }
    } catch (SecurityException e) {
        // ACCESS_NETWORK_STATE permission not granted in the manifest
        return PlayHaven.ConnectionType.NO_PERMISSION;
    }

    return PlayHaven.ConnectionType.NO_NETWORK;
}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {/*w ww  .  j  av  a 2 s. co  m*/

        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null) {

                isNetworkAvailable = netInfo.isConnected();
                //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
            }
        }
    } catch (Exception ex) {
        //Log.e("Network Avail Error", ex.getMessage());

    }
    //        check for wifi also
    if (!isNetworkAvailable) {

        WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        State wifi = cm.getNetworkInfo(1).getState();
        if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
            isNetworkAvailable = true;
            //Log.d("WIFI NETWORK<<","WIFI connected");
        } else {

            isNetworkAvailable = false;
            // Log.d("WIFI Network<<","WIFI not connected");
        }

    }
    return isNetworkAvailable;

}

From source file:util.Utils.java

/**
 * ?wifi?/*from   w ww .j  ava  2  s .  com*/
 * @param context 
 * @return true WiFifalse ?WiFi
 */
public static boolean getWIFIConnectStatus(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi.isConnected();
}

From source file:com.p2p.misc.DeviceUtility.java

public static boolean CheckNetConnectivity(Context mContext) {

    ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connec.getNetworkInfo(0).getState() == State.CONNECTED
            || connec.getNetworkInfo(1).getState() == State.CONNECTED)
        return true;

    return false;

}

From source file:com.dmbstream.android.util.Util.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return wifi != null && wifi.isConnected();
}