Java Code Examples for android.net.NetworkInfo.State

The following are top voted examples for showing how to use android.net.NetworkInfo.State. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: GitHub   File: ConnectivityReceiver.java   View source code 7 votes vote down vote up
public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
	mTerminalManager = manager;

	final ConnectivityManager cm =
			(ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE);

	final WifiManager wm = (WifiManager) manager.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	mWifiLock = wm.createWifiLock(TAG);

	final NetworkInfo info = cm.getActiveNetworkInfo();
	if (info != null) {
		mIsConnected = (info.getState() == State.CONNECTED);
	}

	mLockingWifi = lockingWifi;

	final IntentFilter filter = new IntentFilter();
	filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
	manager.registerReceiver(this, filter);
}
 
Example 2
Project: GitHub   File: ConnectivityReceiver.java   View source code 7 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	final String action = intent.getAction();

	if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
		Log.w(TAG, "onReceived() called: " + intent);
		return;
	}

	boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
	boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

	Log.d(TAG, "onReceived() called; noConnectivity? " + noConnectivity + "; isFailover? " + isFailover);

	if (noConnectivity && !isFailover && mIsConnected) {
		mIsConnected = false;
		mTerminalManager.onConnectivityLost();
	} else if (!mIsConnected) {
		NetworkInfo info = (NetworkInfo) intent.getExtras()
				.get(ConnectivityManager.EXTRA_NETWORK_INFO);

		if (mIsConnected = (info.getState() == State.CONNECTED)) {
			mTerminalManager.onConnectivityRestored();
		}
	}
}
 
Example 3
Project: boohee_v5.6   File: DeviceConfig.java   View source code 7 votes vote down vote up
public static String[] getNetworkAccessMode(Context context) {
    String[] strArr = new String[]{UNKNOW, UNKNOW};
    if (context.getPackageManager().checkPermission("android.permission" +
            ".ACCESS_NETWORK_STATE", context.getPackageName()) != 0) {
        strArr[0] = UNKNOW;
        return strArr;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService
            ("connectivity");
    if (connectivityManager == null) {
        strArr[0] = UNKNOW;
        return strArr;
    } else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
        strArr[0] = "Wi-Fi";
        return strArr;
    } else {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
        if (networkInfo.getState() != State.CONNECTED) {
            return strArr;
        }
        strArr[0] = "2G/3G";
        strArr[1] = networkInfo.getSubtypeName();
        return strArr;
    }
}
 
Example 4
Project: letv   File: bt.java   View source code 6 votes vote down vote up
public static String[] j(Context context) {
    String[] strArr = new String[]{"", ""};
    try {
        if (context.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", context.getPackageName()) != 0) {
            strArr[0] = "";
            return strArr;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            strArr[0] = "";
            return strArr;
        } else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
            strArr[0] = d;
            return strArr;
        } else {
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
            if (networkInfo.getState() == State.CONNECTED) {
                strArr[0] = c;
                strArr[1] = networkInfo.getSubtypeName();
                return strArr;
            }
            return strArr;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Project: buildAPKsApps   File: MobileDataSettingHandler2.java   View source code 6 votes vote down vote up
void updateState(State state) {
	
	if (state != State.DISCONNECTED) {
		mTryCounter = 0;
	}
	
	Setting s = mSetting; int resId;
	switch (state) {
		case CONNECTED: s.checked = true; s.enabled = true; resId = R.string.txt_net_status_connected; break;
		case CONNECTING: s.checked = false; s.enabled = false; resId = R.string.txt_net_status_connecting; break;
		case DISCONNECTED: s.checked = false; s.enabled = true; resId = R.string.txt_net_status_disconnected; break;
		case DISCONNECTING: s.checked = true; s.enabled = false; resId = R.string.txt_net_status_disconnecting; break;
		default: s.checked = s.enabled = false; resId = R.string.txt_status_unknown; break;
	}
	s.descr = getString(resId);
	s.updateView();
}
 
Example 6
Project: FreeStreams-TVLauncher   File: NetWorkUtil.java   View source code 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 7
Project: boohee_v5.6   File: bt.java   View source code 6 votes vote down vote up
public static String[] k(Context context) {
    String[] strArr = new String[]{"", ""};
    try {
        if (a(context, "android.permission.ACCESS_NETWORK_STATE")) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null) {
                strArr[0] = "";
                return strArr;
            } else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
                strArr[0] = d;
                return strArr;
            } else {
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
                if (networkInfo.getState() == State.CONNECTED) {
                    strArr[0] = c;
                    strArr[1] = networkInfo.getSubtypeName();
                    return strArr;
                }
                return strArr;
            }
        }
        strArr[0] = "";
        return strArr;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Project: boohee_v5.6   File: k.java   View source code 6 votes vote down vote up
public static boolean a(Context context) {
    if (!o.a(context, "android.permission.INTERNET")) {
        return false;
    }
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService("connectivity");
        if (connectivityManager == null) {
            return false;
        }
        Object allNetworkInfo = connectivityManager.getAllNetworkInfo();
        if (l.b(allNetworkInfo)) {
            for (NetworkInfo state : allNetworkInfo) {
                if (state.getState() == State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    Toast.makeText(context, "Unable to connect to the network.", 0).show();
    return false;
}
 
Example 9
Project: boohee_v5.6   File: HttpUtils.java   View source code 6 votes vote down vote up
public static boolean isNetworkAvailable(Context context) {
    if (context != null) {
        NetworkInfo[] info = ((ConnectivityManager) context.getSystemService("connectivity"))
                .getAllNetworkInfo();
        if (info != null) {
            for (NetworkInfo state : info) {
                if (state.getState() == State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 10
Project: boohee_v5.6   File: Util.java   View source code 6 votes vote down vote up
public static boolean isWifiEnabled(Context context) {
    ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService
            ("connectivity");
    return (mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState
            () == State.CONNECTED) || ((TelephonyManager) context.getSystemService("phone"))
            .getNetworkType() == 3;
}
 
Example 11
Project: MegviiLicMgr-Android-SDK   File: NetWorkHelper.java   View source code 6 votes vote down vote up
/**
 * 获取网络连接状态
 */
public static int getNetworkType(Context context) {
    // NetworkInfo 有以下方法
    // getDetailedState():获取详细状态。
    // getExtraInfo():获取附加信息(3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap)
    // getReason():获取连接失败的原因。
    // getType():获取网络类型(一般为移动或Wi-Fi)。
    // getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
    // isAvailable():判断该网络是否可用。
    // isConnected():判断是否已经连接。
    // isConnectedOrConnecting():判断是否已经连接或正在连接。
    // isFailover():判断是否连接失败。
    // isRoaming():判断是否漫游

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
    if (networkInfo != null) {
        int type = networkInfo.getType();
        State state = networkInfo.getState();

        if (type == ConnectivityManager.TYPE_WIFI
                && state == State.CONNECTED) {
            // wifi连接通畅
            return NETWORK_CONNETED_WIFI;
        } else if (type == ConnectivityManager.TYPE_MOBILE
                && state == State.CONNECTED) {
            return NETWORK_CONNETED_GPRS;
        }
    }
    return NETWORK_DISCONNETED;
}
 
Example 12
Project: ElephantReader   File: DeviceUtil.java   View source code 6 votes vote down vote up
/**
 * 判断Android客户端网络是否连接
 * 只能判断是否有可用的连接,而不能判断是否能连网
 *
 * @param context
 * @return true/false
 */
public static boolean checkNet(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}
 
Example 13
Project: InfoQ-Android-App   File: NetWorkUtil.java   View source code 6 votes vote down vote up
/**
 * 获取当前网络状态的类型
 * @return 返回网络类型
 */
public NetType getCurrentNetType() {
    ConnectivityManager connManager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // wifi
    NetworkInfo gprs = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // gprs

    if (wifi != null && wifi.getState() == State.CONNECTED) {
         Log.d(TAG, "getCurrentNetType | Current net type:  WIFI.");
        return NetType.NETWORK_TYPE_WIFI;
    } else if (gprs != null && gprs.getState() == State.CONNECTED) {
         Log.d(TAG, "getCurrentNetType | Current net type:  MOBILE.");
        return NetType.NETWOKR_TYPE_MOBILE;
    } else {
         Log.d(TAG, "getCurrentNetType | Current net type:  NONE.");
        return NetType.NETWORK_TYPE_NONE;
    }
}
 
Example 14
Project: InfoQ-Android-App   File: NetWorkUtil.java   View source code 6 votes vote down vote up
/**
 * 判断网络是否连接
 *
 * @param
 * @return
 */
public static boolean isConnected() {

    ConnectivityManager connectivity = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null != connectivity) {

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {

                return true;
            }
        }
    }
    return false;
}
 
Example 15
Project: C.   File: NetUtil.java   View source code 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 16
Project: BigApp_WordPress_Android   File: CommonUtils.java   View source code 6 votes vote down vote up
/***
 * 检查网络
 *
 * @param context Context
 * @param toast   是否需要toast提示
 * @return true or false
 */
public static boolean checkNetworkEnable(Context context, boolean toast) {
    if (context == null) {
        return false;
    }
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info != null && info.isConnected()
            && info.getState() == State.CONNECTED) {
        return true;
    }
    if (toast) {
        ZToastUtils.toastNoNetWork(context);
    }
    return false;
}
 
Example 17
Project: BigApp_WordPress_Android   File: CommonUtils.java   View source code 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 18
Project: connectbot   File: ConnectivityReceiver.java   View source code 6 votes vote down vote up
public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
	mTerminalManager = manager;

	final ConnectivityManager cm =
			(ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE);

	final WifiManager wm = (WifiManager) manager.getSystemService(Context.WIFI_SERVICE);
	mWifiLock = wm.createWifiLock(TAG);

	final NetworkInfo info = cm.getActiveNetworkInfo();
	if (info != null) {
		mIsConnected = (info.getState() == State.CONNECTED);
	}

	mLockingWifi = lockingWifi;

	final IntentFilter filter = new IntentFilter();
	filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
	manager.registerReceiver(this, filter);
}
 
Example 19
Project: connectbot   File: ConnectivityReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	final String action = intent.getAction();

	if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
           Log.w(TAG, "onReceived() called: " + intent);
           return;
	}

	boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
	boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

	Log.d(TAG, "onReceived() called; noConnectivity? " + noConnectivity + "; isFailover? " + isFailover);

	if (noConnectivity && !isFailover && mIsConnected) {
		mIsConnected = false;
		mTerminalManager.onConnectivityLost();
	} else if (!mIsConnected) {
		NetworkInfo info = (NetworkInfo) intent.getExtras()
				.get(ConnectivityManager.EXTRA_NETWORK_INFO);

		if (mIsConnected = (info.getState() == State.CONNECTED)) {
			mTerminalManager.onConnectivityRestored();
		}
	}
}
 
Example 20
Project: AndroidStudyDemo   File: NetUtil.java   View source code 6 votes vote down vote up
/**
 * 判断网络连接是否打开,包括移动数据连接
 *
 * @param context 上下文
 * @return 是否联网
 */
public static boolean isNetworkAvailable(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {

        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {

                if (info[i].getState() == State.CONNECTED) {

                    netstate = true;
                    break;
                }
            }
        }
    }
    return netstate;
}
 
Example 21
Project: youkes_vr   File: NetUtil.java   View source code 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 22
Project: edx-app-android   File: NetworkUtil.java   View source code 6 votes vote down vote up
/**
 * Returns true if device is connected to wifi or mobile network, false
 * otherwise.
 *
 * @param context
 * @return
 */
public static boolean isConnected(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo infoWifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (infoWifi != null) {
        State wifi = infoWifi.getState();
        if (wifi == NetworkInfo.State.CONNECTED) {
            logger.debug("Wifi is connected");
            return true;
        }
    }

    NetworkInfo infoMobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (infoMobile != null) {
        State mobile = infoMobile.getState();
        if (mobile == NetworkInfo.State.CONNECTED) {
            logger.debug("Mobile data is connected");
            return true;
        }
    }

    logger.debug("Network not available");
    return false;
}
 
Example 23
Project: android-tv-launcher   File: NetWorkUtil.java   View source code 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 24
Project: XmppTest   File: NetUtil.java   View source code 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 25
Project: AndroidPAD   File: NetworkUtils.java   View source code 6 votes vote down vote up
/**
 * 检查网络状态
 * 
 * @param context
 * @return
 */
public static boolean checkNetState(Context context) {
	boolean netstate = false;
	ConnectivityManager connectivity = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connectivity != null) {
		NetworkInfo[] info = connectivity.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					netstate = true;
					break;
				}
			}
		}
	}
	return netstate;
}
 
Example 26
Project: AndroidPAD   File: NetworkUtils.java   View source code 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 27
Project: Fon   File: FonManWifiReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(final Context c, final Intent i) {
	final String a = i.getAction();
	if (a.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) && FonManService.isAutoConnectEnabled(c)) {
		FonManService.execute(c, Constants.ACT_CONNECT);
	} else if (a.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
		final NetworkInfo ni = (NetworkInfo) i.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
		if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
			final State s = ni.getState();
			if (s == State.CONNECTED) {
				FonManService.execute(c, Constants.ACT_LOGIN);
			} else if (s == State.DISCONNECTED) {
				FonManService.execute(c, Constants.ACT_CLEANUP);
			}
		}
	}
}
 
Example 28
Project: GotyeSDK-Android   File: GotyeService.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	boolean success = false;
	Log.d("", "check login");
	// 获得网络连接服务
	ConnectivityManager connManager = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	// State state = connManager.getActiveNetworkInfo().getState();
	State state = connManager.getNetworkInfo(
			ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
	if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
		success = true;
	}

	state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
			.getState(); // 获取网络连接状态
	if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
		success = true;
	}
	if (success) {
		KeepAlive.startKeepAlive(context);
	}else {
		//没有网络停止检查登录状态
		KeepAlive.stopKeepAlive(context);
	}
}
 
Example 29
Project: lookingforseats-nwpu   File: NetInfo.java   View source code 6 votes vote down vote up
public boolean isNetConnect(Context context){
	        boolean success = false;
			//获得网络连接服务
			ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			// State state = connManager.getActiveNetworkInfo().getState();
			State state = connManager.getNetworkInfo(
			        ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
			if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
			    success = true;
			}
			
			state = connManager.getNetworkInfo(
			        ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
			if (State.CONNECTED == state) { // 判断是否正在使用GPRS网络
			    success = true;
			}
			if(!success)
			{
				Toast.makeText(context, "请检查您的网络连接", Toast.LENGTH_SHORT).show();
			}
			return success;
}
 
Example 30
Project: Office-365-SDK-for-Android   File: NetworkUtils.java   View source code 6 votes vote down vote up
public static int getDataConnectionState(Context context) {
    if (!isSimInstalled(context)) {
        return NETWORK_UTILS_CONNECTION_STATE_DISCONNECTED;
    }

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    for (NetworkInfo info : manager.getAllNetworkInfo()) {
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (info.getState() == State.CONNECTED) {
                return NETWORK_UTILS_CONNECTION_STATE_CONNECTED;
            } else if (info.getState() == State.DISCONNECTED) {
                return NETWORK_UTILS_CONNECTION_STATE_DISCONNECTED;
            } else if (info.getState() == State.CONNECTING) {
                return NETWORK_UTILS_CONNECTION_STATE_CONNECTING;
            } else if (info.getState() == State.DISCONNECTING) {
                return NETWORK_UTILS_CONNECTION_STATE_DISCONNECTED;
            } else if (info.getState() == State.SUSPENDED) {
                return NETWORK_UTILS_CONNECTION_STATE_SUSPENDED;
            } else if (info.getState() == State.UNKNOWN) {
                return NETWORK_UTILS_CONNECTION_STATE_UNKNOWN;
            }
        }
    }

    return NETWORK_UTILS_CONNECTION_STATE_DISCONNECTED;
}
 
Example 31
Project: androidwisprclient   File: NetworkConnectivityReceiver.java   View source code 6 votes vote down vote up
private boolean isDisconnectedIntent(Intent intent) {
	boolean res = false;
	NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
	// Log.d(TAG, "NetworkInfo:" + networkInfo);

	if (networkInfo != null) {
		State state = networkInfo.getState();
		res = (state.equals(NetworkInfo.State.DISCONNECTING) || state.equals(NetworkInfo.State.DISCONNECTED))
				&& (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
	} else {
		int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
		// Log.d(TAG, "wifiState:" + wifiState);
		if (wifiState == WifiManager.WIFI_STATE_DISABLED || wifiState == WifiManager.WIFI_STATE_DISABLING) {
			res = true;
		}
	}

	return res;
}
 
Example 32
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 33
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 34
Project: Androidpn   File: PhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Log.d(LOGTAG, "PhoneStateReceiver.onReceive()...");
    String action = intent.getAction();
    Log.d(LOGTAG, "action=" + action);        

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (NotificationService.getConnectivityManager(notificationService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        NotificationService.restart(notificationService);
    }

}
 
Example 35
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 36
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 37
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 38
Project: Androidpn   File: SdkPhoneStateReceiver.java   View source code 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    Object parcelableExtra = intent.getParcelableExtra("networkInfo");
    boolean connected = false;
    if (parcelableExtra instanceof NetworkInfo) {
        State state = ((NetworkInfo) parcelableExtra).getState();
        if (state != null) {
            Log.d(LOGTAG, "Wifi data state is " + state.toString());
            if (State.CONNECTED.equals(state)) {
                connected = true;
            }
        }
    } else if (MainService.getConnectivityManager(mainService)
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        connected = true;
    }
    if (connected) {
        MainService.restart(mainService);
    }

}
 
Example 39
Project: EZ-Wifi-Notification   File: IntentAnalyzer.java   View source code 6 votes vote down vote up
private void log(boolean noConnectivity, State affectedState,
		DetailedState detailedState, NetworkInfo mNetworkInfo,
		NetworkInfo mOtherNetworkInfo, String mReason, boolean mIsFailover) {

	// Log
	if (Constants.Debug) {
		//Log.d(TAG, "EXTRA_REASON Flag: " + mReason);
		//Log.d(TAG, "EXTRA_IS_FAILOVER Flag: " + mIsFailover);
		//Log.d(TAG, "EXTRA_NO_CONNECTIVITY Flag: " + noConnectivity);
		// Log.d(TAG, "mNetworkInfo.getState(): "+affectedState);
		// Log.d(TAG,
		// "mNetworkInfo.getDetailedState(): "+detailedState);
		Log.d(TAG, "------ Reciever Called -----------");
		Log.d(TAG, "noConn=" + noConnectivity + ", AffectedState: "
				+ affectedState + ", DetailedState: " + detailedState);
		Log.d(TAG, "mNetworkInfo: " + mNetworkInfo);
		Log.d(TAG, "mOtherNetworkInfo: "
				+ (mOtherNetworkInfo == null ? "[none]"
						: mOtherNetworkInfo));
		State mState = (noConnectivity) ? State.DISCONNECTED
				: State.CONNECTED;
		Log.d(TAG, " mState=" + mState.toString() + ", mReason="
				+ mReason + ", mIsFailover=" + mIsFailover);
	}		
}
 
Example 40
Project: smartedu   File: NetUtility.java   View source code 6 votes vote down vote up
/**
    * 获取当前网络状态
    * @param context
    * @return
    */
   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;
}