android.net.NetworkInfo.State Java Examples

The following examples show how to use android.net.NetworkInfo.State. 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: NetworkStateReceiver.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public void onReceive(Context context, Intent intent) {
    if (ACTION_CONNECTIVITY_CHANGE.equals(intent.getAction()) || intent.getAction().equals(ACTION_WIFISTATE_CHANGE)) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
        State wifiState = cm.getNetworkInfo(1).getState();
        if (cm.getNetworkInfo(0) != null) {
            State mobileState = cm.getNetworkInfo(0).getState();
            if (isNetworkConnected(wifiState)) {
                onNetworkStateChange(NetworkType.Wifi, context);
            } else if (isNetworkConnected(mobileState)) {
                onNetworkStateChange(NetworkType.Mobile, context);
            } else {
                onNetworkStateChange(NetworkType.None, context);
            }
        }
    }
}
 
Example #2
Source File: CommonUtils.java    From BigApp_WordPress_Android with Apache License 2.0 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 #3
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 #4
Source File: NetUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 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 #5
Source File: NetworkUtil.java    From edx-app-android with Apache License 2.0 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 #6
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 #7
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 #8
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 #9
Source File: AbAppUtil.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivity == null) {
            return false;
        }
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.isConnected() && info.getState() == State.CONNECTED) {
            return true;
        }
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example #10
Source File: WiFiUtils.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static boolean netOkay(Context context) {
    int i = 1;
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
    NetworkInfo mMobileNetworkInfo = connectivityManager.getNetworkInfo(0);
    NetworkInfo wifiWorkInfo = connectivityManager.getNetworkInfo(1);
    if (mMobileNetworkInfo == null) {
        return false;
    }
    int i2;
    boolean isAvailable = mMobileNetworkInfo.isAvailable();
    if (mMobileNetworkInfo.getState() == State.CONNECTED) {
        i2 = 1;
    } else {
        i2 = 0;
    }
    i2 &= isAvailable;
    isAvailable = wifiWorkInfo.isAvailable();
    if (wifiWorkInfo.getState() != State.CONNECTED) {
        i = 0;
    }
    return i2 | (i & isAvailable);
}
 
Example #11
Source File: NetworkMonitor.java    From android-utilset with Apache License 2.0 5 votes vote down vote up
private boolean isNetworkDisconnected(State state) {
	switch (state) {
	case DISCONNECTED:
	case DISCONNECTING:
		return true;

	default:
		return false;
	}
}
 
Example #12
Source File: FunctionUtil.java    From BambooPlayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #13
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;
}
 
Example #14
Source File: XmppService.java    From yiim_v2 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	// TODO Auto-generated method stub
	if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent
			.getAction())) {
		// 获取手机的连接服务管理器,这里是连接管理器类
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		mobileState = cm
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();

		if (wifiState != null && mobileState != null
				&& State.CONNECTED != wifiState
				&& State.CONNECTED == mobileState) {
			YiLog.getInstance().i("net 手机网络连接成功!");
			getHandler().removeMessages(MSG_NETWORK_CONNECTED);
			getHandler().sendEmptyMessageDelayed(MSG_NETWORK_CONNECTED,
					400);
		} else if (wifiState != null && mobileState != null
				&& State.CONNECTED == wifiState
				&& State.CONNECTED != mobileState) {
			YiLog.getInstance().i("net 无线网络连接成功!");
			getHandler().removeMessages(MSG_NETWORK_CONNECTED);
			getHandler().sendEmptyMessageDelayed(MSG_NETWORK_CONNECTED,
					400);
		} else if (wifiState != null && mobileState != null
				&& State.CONNECTED != wifiState
				&& State.CONNECTED != mobileState) {
			YiLog.getInstance().i("net 手机没有任何网络...");
			getHandler().removeMessages(MSG_NO_NETWORK);
			getHandler().sendEmptyMessageDelayed(MSG_NO_NETWORK, 400);
		}
	}
}
 
Example #15
Source File: SystemTool.java    From KJFrameForAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否为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;
}
 
Example #16
Source File: SystemTool.java    From Lay-s with MIT License 5 votes vote down vote up
/**
 * 判断是否为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;
}
 
Example #17
Source File: SystemUtils.java    From VideoMeeting with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否为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;
}
 
Example #18
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 #19
Source File: NetHelper.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
public static boolean connectionPresent (final Context context) {
	final ConnectivityManager cMgr = getConnectivityManager(context);
	if (cMgr == null) return false;

	final NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
	if (netInfo == null) return false;

	final State state = netInfo.getState();
	if (state == null) return false;

	return state.equals(State.CONNECTED);
}
 
Example #20
Source File: StatusServiceImpl.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    State wifiState = null;
    State mobileState = null;
    final ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfoWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfoWifi != null) {
        wifiState = networkInfoWifi.getState();
    }

    final NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (networkInfo != null) {
        mobileState = networkInfo.getState();
    }

    if (wifiState != null && mobileState != null && State.CONNECTED != wifiState
            && State.CONNECTED == mobileState) {
        // 手机网络连接成功
        if (mDebug) Log.d(TAG, "手机网络连接成功 ");
        if (!mListeners.isEmpty()) {
            notifyNetworkTo3G(context);
        }
    } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState
            && State.CONNECTED != mobileState) {
        // 手机没有任何的网络
        if (mDebug) Log.d(TAG, "手机没有任何的网络,网络中断 ");
        if (!mListeners.isEmpty()) {
            notifyNetworkDisconnect(context);
        }
    } else if (wifiState != null && State.CONNECTED == wifiState) {
        // 无线网络连接成功
        if (mDebug) Log.d(TAG, " 无线网络连接成功");
        if (!mListeners.isEmpty()) {
            notifyNetworkConnect(context);
        }
    }

}
 
Example #21
Source File: MPushReceiver.java    From mpush-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (ACTION_HEALTH_CHECK.equals(action)) {//处理心跳
        if (MPush.I.hasStarted()) {
            if (MPush.I.client.isRunning()) {
                if (MPush.I.client.healthCheck()) {
                    startAlarm(context, delay);
                }
            }
        }
    } else if (CONNECTIVITY_ACTION.equals(action)) {//处理网络变化
        if (hasNetwork(context)) {
            if (STATE != State.CONNECTED) {
                STATE = State.CONNECTED;
                if (MPush.I.hasStarted()) {
                    MPush.I.onNetStateChange(true);

                    //MPush.I.resumePush();
                } else {
                    MPush.I.checkInit(context).startPush();
                }
            }
        } else {
            if (STATE != State.DISCONNECTED) {
                STATE = State.DISCONNECTED;
                MPush.I.onNetStateChange(false);

                //MPush.I.pausePush();
                //cancelAlarm(context);//防止特殊场景下alarm没被取消
            }
        }
    } else if (ACTION_NOTIFY_CANCEL.equals(action)) {//处理通知取消
        Notifications.I.clean(intent);
    }
}
 
Example #22
Source File: SystemTool.java    From Common with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否为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;
}
 
Example #23
Source File: WiFiUtils.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public static boolean isPhoneNet(Context context) {
    int i = 0;
    NetworkInfo mMobileNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getNetworkInfo(0);
    if (mMobileNetworkInfo == null) {
        return false;
    }
    boolean isAvailable = mMobileNetworkInfo.isAvailable();
    if (mMobileNetworkInfo.getState() == State.CONNECTED) {
        i = 1;
    }
    return i & isAvailable;
}
 
Example #24
Source File: bt.java    From letv with Apache License 2.0 5 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 #25
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 #26
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 #27
Source File: SystemTool.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 判断是否为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;
}
 
Example #28
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 #29
Source File: NetWorkUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否连接了网络
 * @return {@code true} yes, {@code false} no
 */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public static boolean isConnect() {
    try {
        // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 )
        ConnectivityManager cManager = AppUtils.getConnectivityManager();
        // 版本兼容处理
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            // 获取网络连接管理的对象
            NetworkInfo nInfo = cManager.getActiveNetworkInfo();
            // 判断是否为 null
            if (nInfo != null) {
                // 判断当前网络是否已经连接
                if (nInfo.getState() == State.CONNECTED) {
                    return true;
                }
            }
        } else {
            // 获取当前活跃的网络 ( 连接的网络信息 )
            Network network = cManager.getActiveNetwork();
            // 判断是否为 null
            if (network != null) {
                return true;
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "isConnect");
    }
    return false;
}
 
Example #30
Source File: NetworkStateReceiver.java    From FimiX8-RE with MIT License 4 votes vote down vote up
private boolean isNetworkConnected(State state) {
    if (state == null || State.CONNECTED != state) {
        return false;
    }
    return true;
}