Search in sources :

Example 16 with State

use of android.net.NetworkInfo.State in project teaTime by ancfdy.

the class AppNetworkMgr method isNetworkConnected.

/**
     * 判断网络是否连接
     *
     * @param context 上下文
     * @return boolean 网络连接状态
     */
public static boolean isNetworkConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        //获取连接对象
        if (mNetworkInfo != null) {
            //判断是TYPE_MOBILE网络
            if (ConnectivityManager.TYPE_MOBILE == mNetworkInfo.getType()) {
                AppLogMessageMgr.i("AppNetworkMgr", "网络连接类型为:TYPE_MOBILE");
                //判断移动网络连接状态
                State STATE_MOBILE = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
                if (STATE_MOBILE == State.CONNECTED) {
                    AppLogMessageMgr.i("AppNetworkMgrd", "网络连接类型为:TYPE_MOBILE, 网络连接状态CONNECTED成功!");
                    return mNetworkInfo.isAvailable();
                }
            }
            //判断是TYPE_WIFI网络
            if (ConnectivityManager.TYPE_WIFI == mNetworkInfo.getType()) {
                AppLogMessageMgr.i("AppNetworkMgr", "网络连接类型为:TYPE_WIFI");
                //判断WIFI网络状态
                State STATE_WIFI = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
                if (STATE_WIFI == State.CONNECTED) {
                    AppLogMessageMgr.i("AppNetworkMgr", "网络连接类型为:TYPE_WIFI, 网络连接状态CONNECTED成功!");
                    return mNetworkInfo.isAvailable();
                }
            }
        }
    }
    return false;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) State(android.net.NetworkInfo.State)

Example 17 with State

use of android.net.NetworkInfo.State in project android_frameworks_base by crdroidandroid.

the class NetworkState method validateNetworkConnection.

/**
     * Verify the network state to connection
     * @return false if any of the state transitions were not valid
     */
private boolean validateNetworkConnection() {
    StringBuffer str = new StringBuffer("States ");
    str.append(printStates());
    if (mStateDepository.get(0) != State.DISCONNECTED) {
        str.append(String.format(" Initial state should be DISCONNECTED, but it is %s.", mStateDepository.get(0)));
        mReason = str.toString();
        return false;
    }
    State lastState = mStateDepository.get(mStateDepository.size() - 1);
    if (lastState != mTransitionTarget) {
        str.append(String.format(" Last state should be %s, but it is %s", mTransitionTarget, lastState));
        mReason = str.toString();
        return false;
    }
    for (int i = 1; i < mStateDepository.size(); i++) {
        State preState = mStateDepository.get(i - 1);
        State curState = mStateDepository.get(i);
        if ((preState == State.DISCONNECTED) && ((curState == State.CONNECTING) || (curState == State.CONNECTED) || (curState == State.DISCONNECTED))) {
            continue;
        } else if ((preState == State.CONNECTING) && (curState == State.CONNECTED)) {
            continue;
        } else if ((preState == State.CONNECTED) && (curState == State.CONNECTED)) {
            continue;
        } else {
            str.append(String.format(" Transition state from %s to %s is not valid.", preState.toString(), curState.toString()));
            mReason = str.toString();
            return false;
        }
    }
    mReason = str.toString();
    return true;
}
Also used : State(android.net.NetworkInfo.State)

Aggregations

State (android.net.NetworkInfo.State)17 ConnectivityManager (android.net.ConnectivityManager)3 NetworkInfo (android.net.NetworkInfo)2