Java Code Examples for android.net.NetworkInfo.State#CONNECTING

The following examples show how to use android.net.NetworkInfo.State#CONNECTING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonUtils.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
/**
 * @param context Context
 * @return 1-wifi, 2-3G, 0-无网络连接
 */
public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State mobileState = connectivityManager.getNetworkInfo(
            ConnectivityManager.TYPE_MOBILE).getState();
    State wifiState = connectivityManager.getNetworkInfo(
            ConnectivityManager.TYPE_WIFI).getState();
    if (wifiState == State.CONNECTED || wifiState == State.CONNECTING) {
        return 1;
    } else if (mobileState == State.CONNECTED
            || mobileState == State.CONNECTING) {
        return 2;
    } else {
        return 0;
    }
}
 
Example 2
Source File: NetWorkUtil.java    From android-tv-launcher with MIT License 6 votes vote down vote up
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 STATE_WIFI;
	}

	// 3G
	state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
			.getState();
	if (state == State.CONNECTED || state == State.CONNECTING) {
		return STATE_MOBILE;
	}
	return STATE_DISCONNECT;
}
 
Example 3
Source File: NetUtil.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: NetUtil.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: NetWorkUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取连接的网络类型
 * @return -1 = 等于未知, 1 = Wifi, 2 = 移动网络
 */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public static int getConnectType() {
    try {
        // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 )
        ConnectivityManager cManager = AppUtils.getConnectivityManager();
        // 版本兼容处理
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            // 判断连接的是否 Wifi
            State wifiState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            // 判断是否连接上
            if (wifiState == State.CONNECTED || wifiState == State.CONNECTING) {
                return 1;
            } else {
                // 判断连接的是否移动网络
                State mobileState = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
                // 判断移动网络是否连接上
                if (mobileState == State.CONNECTED || mobileState == State.CONNECTING) {
                    return 2;
                }
            }
        } else {
            // 获取当前活跃的网络 ( 连接的网络信息 )
            Network network = cManager.getActiveNetwork();
            if (network != null) {
                NetworkCapabilities networkCapabilities = cManager.getNetworkCapabilities(network);
                // 判断连接的是否 Wifi
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return 1;
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                    // 判断连接的是否移动网络
                    return 2;
                }
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getConnectType");
    }
    return -1;
}
 
Example 6
Source File: VMNetwork.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 网络已经连接情况下,去判断是 WIFI 还是 GPRS
 * 可以根据返回情况做一些自己的逻辑调用
 */
private boolean isGPRSNetwork() {
    State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
        return true;
    }
    return false;
}
 
Example 7
Source File: VMNetwork.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 网络已经连接情况下,去判断是 WIFI 还是 GPRS
 * 可以根据返回情况做一些自己的逻辑调用
 */
private boolean isWIFINetwork() {
    State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
        return true;
    }
    return false;
}
 
Example 8
Source File: NetUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 只是判断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;

}
 
Example 9
Source File: ZenAdsManager.java    From zen4android with MIT License 5 votes vote down vote up
private boolean isWifiAvailable() {
	Context context = ZenApplication.getAppContext();
	ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

	State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
			.getState();
	if(wifi == State.CONNECTED || wifi == State.CONNECTING) {
		System.out.println("wifi available");
		return true;
	}
	return false;
}