Code example for NetworkInfo

Methods: getExtraInfo, getState, isConnected

0
 
	// 由于检查网络是否连接是某一瞬间的动作,故写成一方法 
	public String getNetworkType() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null && activeNetInfo.isConnected()) {
			if (activeNetInfo.getState() == NetworkInfo.State.CONNECTED) {
				isConnected = true; // 判断是否联网
			} 
			return activeNetInfo.getExtraInfo(); // 接入点名称: 此名称可被用户任意更改 如: cmwap,
		} else { 
			return null; 
		} 
	} 
 
	// 检查是否已经登陆 
	public boolean checkIsLogedIn() { 
		if (tmPref.contains(Preferences.CURRENT_USER_ID)
				&& tmPref.getString(Preferences.CURRENT_USER_ID, "00000000-0000-0000-0000-000000000000") != "00000000-0000-0000-0000-000000000000") {
			return true; 
		} else