android.net.NetworkInfo.State

Here are the examples of the java api class android.net.NetworkInfo.State taken from open source projects.

1. NetUtil#getNetworkState()

Project: WayHoo
File: NetUtil.java
public static int getNetworkState(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // Wifi
    State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_WIFI;
    }
    // 3G
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_MOBILE;
    }
    return NETWORN_NONE;
}

2. NetUtil#getNetworkState()

Project: WayHoo
File: NetUtil.java
public static int getNetworkState(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // Wifi
    State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_WIFI;
    }
    // 3G
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_MOBILE;
    }
    return NETWORN_NONE;
}

3. OrgUtils#isMobileOnline()

Project: mobileorg-android
File: OrgUtils.java
public static boolean isMobileOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (mobile == NetworkInfo.State.CONNECTED)
        return true;
    else
        return false;
}

4. OrgUtils#isWifiOnline()

Project: mobileorg-android
File: OrgUtils.java
public static boolean isWifiOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == NetworkInfo.State.CONNECTED)
        return true;
    else
        return false;
}

5. VDUtility#isOnlyMobileType()

Project: NewsMe
File: VDUtility.java
public static boolean isOnlyMobileType(Context context) {
    State wifiState = null;
    State mobileState = null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        wifiState = networkInfo.getState();
    }
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        mobileState = networkInfo.getState();
    }
    VDLog.d("zhang", "onReceive -- wifiState = " + wifiState + " -- mobileState = " + mobileState);
    if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
        // ????????
        VDLog.d("zhang", "onReceive -- ????????");
        return true;
    }
    return false;
}

6. VDNetworkBroadcastReceiver#onReceive()

Project: NewsMe
File: VDNetworkBroadcastReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    State wifiState = null;
    State mobileState = null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        VDLog.w(TAG, "onReceive -- ConnectivityManager is null!");
        return;
    }
    NetworkInfo networkInfo = null;
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        wifiState = networkInfo.getState();
    }
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        mobileState = networkInfo.getState();
    }
    VDLog.d(TAG, "onReceive -- wifiState = " + wifiState + " -- mobileState = " + mobileState);
    if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
        // ????????
        VDLog.d(TAG, "onReceive -- ????????");
        mobileNotify();
    } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED != mobileState) {
        // ?????????
        VDLog.d(TAG, "onReceive -- ?????????");
        nothingNotify();
    } else if (wifiState != null && State.CONNECTED == wifiState) {
        // ????????
        VDLog.d(TAG, "onReceive -- ????????");
        wifiNotify();
    }
}

7. AndroidUtil#isOnlyMobileType()

Project: NewsMe
File: AndroidUtil.java
/**
	 * ??????????
	 * 
	 * @param context
	 * @return
	 */
public static boolean isOnlyMobileType(Context context) {
    State wifiState = null;
    State mobileState = null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        wifiState = networkInfo.getState();
    }
    try {
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (networkInfo != null) {
        mobileState = networkInfo.getState();
    }
    LogS.d("zhang", "onReceive -- wifiState = " + wifiState + " -- mobileState = " + mobileState);
    if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
        // ????????
        LogS.d("zhang", "onReceive -- ????????");
        return true;
    }
    return false;
}

8. SystemTool#isWiFi()

Project: KJFrameForAndroid
File: SystemTool.java
/**
     * ?????wifi??
     */
public static boolean isWiFi(Context cxt) {
    ConnectivityManager cm = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
    // wifi????ConnectivityManager.TYPE_WIFI
    // 3G????ConnectivityManager.TYPE_MOBILE
    State state = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    return State.CONNECTED == state;
}

9. FunctionUtil#isInWifi()

Project: BambooPlayer
File: FunctionUtil.java
public static boolean isInWifi(Context mContext) {
    ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    return wifi == State.CONNECTED;
}

10. NetUtil#isWiFi()

Project: AndroidStudyDemo
File: NetUtil.java
/**
     * ????WIFI
     *
     * @param context ???
     * @return ????Wifi
     */
public static boolean isWiFi(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == State.CONNECTED || wifi == State.CONNECTING)
        return true;
    return false;
}