Java Code Examples for android.net.NetworkInfo.State#CONNECTED
The following examples show how to use
android.net.NetworkInfo.State#CONNECTED .
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: NetWorkUtil.java From android-tv-launcher with MIT License | 6 votes |
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 2
Source File: NetUtil.java From AndroidStudyDemo with GNU General Public License v2.0 | 6 votes |
/** * 判断网络连接是否打开,包括移动数据连接 * * @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 3
Source File: CommonUtils.java From BigApp_WordPress_Android with Apache License 2.0 | 6 votes |
/** * @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 WayHoo with Apache License 2.0 | 6 votes |
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: AbAppUtil.java From FimiX8-RE with MIT License | 6 votes |
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 6
Source File: ZenAdsManager.java From zen4android with MIT License | 5 votes |
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 7
Source File: NetWorkUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 判断是否连接了网络 * @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 8
Source File: NetWorkUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 获取连接的网络类型 * @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 9
Source File: XmppService.java From yiim_v2 with GNU General Public License v2.0 | 5 votes |
@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 10
Source File: SystemUtils.java From VideoMeeting with Apache License 2.0 | 5 votes |
/** * 判断是否为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 11
Source File: bt.java From letv with Apache License 2.0 | 5 votes |
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 12
Source File: SystemTool.java From Lay-s with MIT License | 5 votes |
/** * 判断是否为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 13
Source File: SystemTool.java From KJFrameForAndroid with Apache License 2.0 | 5 votes |
/** * 判断是否为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 14
Source File: DeviceStateReceiver.java From SimpleOpenVpn-Android with Apache License 2.0 | 4 votes |
public void networkStateChange(Context context) { NetworkInfo networkInfo = getCurrentNetworkInfo(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); String netstatestring; if (networkInfo == null) { netstatestring = "not connected"; } else { String subtype = networkInfo.getSubtypeName(); if (subtype == null) subtype = ""; String extrainfo = networkInfo.getExtraInfo(); if (extrainfo == null) extrainfo = ""; /* if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiMgr.getConnectionInfo(); extrainfo+=wifiinfo.getBSSID(); subtype += wifiinfo.getNetworkId(); }*/ netstatestring = String.format("%2$s %4$s to %1$s %3$s", networkInfo.getTypeName(), networkInfo.getDetailedState(), extrainfo, subtype); } if (networkInfo != null && networkInfo.getState() == State.CONNECTED) { int newnet = networkInfo.getType(); boolean pendingDisconnect = (network == connectState.PENDINGDISCONNECT); network = connectState.SHOULDBECONNECTED; boolean sameNetwork; if (lastConnectedNetwork == null || lastConnectedNetwork.getType() != networkInfo.getType() || !equalsObj(lastConnectedNetwork.getExtraInfo(), networkInfo.getExtraInfo()) ) sameNetwork = false; else sameNetwork = true; /* Same network, connection still 'established' */ if (pendingDisconnect && sameNetwork) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); // Reprotect the sockets just be sure mManagement.networkChange(true); } else { /* Different network or connection not established anymore */ if (screen == connectState.PENDINGDISCONNECT) screen = connectState.DISCONNECTED; if (shouldBeConnected()) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); if (pendingDisconnect || !sameNetwork) mManagement.networkChange(sameNetwork); else mManagement.resume(); } lastNetwork = newnet; lastConnectedNetwork = networkInfo; } } else if (networkInfo == null) { // Not connected, stop openvpn, set last connected network to no network lastNetwork = -1; if (sendusr1) { network = connectState.PENDINGDISCONNECT; mDisconnectHandler.postDelayed(mDelayDisconnectRunnable, DISCONNECT_WAIT * 1000); } } if (!netstatestring.equals(lastStateMsg)) VpnStatus.logInfo(R.string.netstatus, netstatestring); lastStateMsg = netstatestring; }
Example 15
Source File: MessageUtil.java From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 | 4 votes |
public static boolean isInWifi() { ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); return wifi == State.CONNECTED; }
Example 16
Source File: DeviceStateReceiver.java From Cybernet-VPN with GNU General Public License v3.0 | 4 votes |
public void networkStateChange(Context context) { NetworkInfo networkInfo = getCurrentNetworkInfo(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); String netstatestring; if (networkInfo == null) { netstatestring = "not connected"; } else { String subtype = networkInfo.getSubtypeName(); if (subtype == null) subtype = ""; String extrainfo = networkInfo.getExtraInfo(); if (extrainfo == null) extrainfo = ""; /* if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiMgr.getConnectionInfo(); extrainfo+=wifiinfo.getBSSID(); subtype += wifiinfo.getNetworkId(); }*/ netstatestring = String.format("%2$s %4$s to %1$s %3$s", networkInfo.getTypeName(), networkInfo.getDetailedState(), extrainfo, subtype); } if (networkInfo != null && networkInfo.getState() == State.CONNECTED) { int newnet = networkInfo.getType(); boolean pendingDisconnect = (network == connectState.PENDINGDISCONNECT); network = connectState.SHOULDBECONNECTED; boolean sameNetwork; if (lastConnectedNetwork == null || lastConnectedNetwork.getType() != networkInfo.getType() || !equalsObj(lastConnectedNetwork.getExtraInfo(), networkInfo.getExtraInfo())) sameNetwork = false; else sameNetwork = true; /* Same network, connection still 'established' */ if (pendingDisconnect && sameNetwork) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); // Reprotect the sockets just be sure mManagement.networkChange(true); } else { /* Different network or connection not established anymore */ if (screen == connectState.PENDINGDISCONNECT) screen = connectState.DISCONNECTED; if (shouldBeConnected()) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); if (pendingDisconnect || !sameNetwork) mManagement.networkChange(sameNetwork); else mManagement.resume(); } lastNetwork = newnet; lastConnectedNetwork = networkInfo; } } else if (networkInfo == null) { // Not connected, stop openvpn, set last connected network to no network lastNetwork = -1; if (sendusr1) { network = connectState.PENDINGDISCONNECT; mDisconnectHandler.postDelayed(mDelayDisconnectRunnable, DISCONNECT_WAIT * 1000); } } if (!netstatestring.equals(lastStateMsg)) VpnStatus.logInfo(R.string.netstatus, netstatestring); lastStateMsg = netstatestring; }
Example 17
Source File: NetworkMonitor.java From android-utilset with Apache License 2.0 | 4 votes |
private void initNetworkState(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo niMobile = cm.getNetworkInfo(TYPE_MOBILE); NetworkInfo niWifi = cm.getNetworkInfo(TYPE_WIFI); NetworkInfo niMms = cm.getNetworkInfo(TYPE_MOBILE_MMS); NetworkInfo niSupl = cm.getNetworkInfo(TYPE_MOBILE_SUPL); NetworkInfo niDun = cm.getNetworkInfo(TYPE_MOBILE_DUN); NetworkInfo niHipri = cm.getNetworkInfo(TYPE_MOBILE_HIPRI); NetworkInfo niWimax = cm.getNetworkInfo(TYPE_WIMAX); NetworkInfo niBlueTooth = cm.getNetworkInfo(TYPE_BLUETOOTH); NetworkInfo niDummy = cm.getNetworkInfo(TYPE_DUMMY); NetworkInfo niEthernet = cm.getNetworkInfo(TYPE_ETHERNET); if (niWifi != null && niWifi.getState() == State.CONNECTED) { state = TYPE_WIFI; handleNetworkConnected(); } else if (niMobile != null && niMobile.getState() == State.CONNECTED) { state = TYPE_MOBILE; handleNetworkConnected(); } else if (niBlueTooth != null && niBlueTooth.getState() == State.CONNECTED) { state = TYPE_BLUETOOTH; handleNetworkConnected(); } else if (niMms != null && niMms.getState() == State.CONNECTED) { state = TYPE_MOBILE_MMS; handleNetworkConnected(); } else if (niSupl != null && niSupl.getState() == State.CONNECTED) { state = TYPE_MOBILE_SUPL; handleNetworkConnected(); } else if (niDun != null && niDun.getState() == State.CONNECTED) { state = TYPE_MOBILE_DUN; handleNetworkConnected(); } else if (niHipri != null && niHipri.getState() == State.CONNECTED) { state = TYPE_MOBILE_HIPRI; handleNetworkConnected(); } else if (niWimax != null && niWimax.getState() == State.CONNECTED) { state = TYPE_WIMAX; handleNetworkConnected(); } else if (niDummy != null && niDummy.getState() == State.CONNECTED) { state = TYPE_DUMMY; handleNetworkConnected(); } else if (niEthernet != null && niEthernet.getState() == State.CONNECTED) { state = TYPE_ETHERNET; handleNetworkConnected(); } }
Example 18
Source File: InstantUploadBroadcastReceiver.java From Cirrus_depricated with GNU General Public License v2.0 | 4 votes |
private static boolean isConnectedViaWiFi(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI && cm.getActiveNetworkInfo().getState() == State.CONNECTED; }
Example 19
Source File: DeviceStateReceiver.java From EasyVPN-Free with GNU General Public License v3.0 | 4 votes |
public void networkStateChange(Context context) { NetworkInfo networkInfo = getCurrentNetworkInfo(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); String netstatestring; if (networkInfo == null) { netstatestring = "not connected"; } else { String subtype = networkInfo.getSubtypeName(); if (subtype == null) subtype = ""; String extrainfo = networkInfo.getExtraInfo(); if (extrainfo == null) extrainfo = ""; /* if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiMgr.getConnectionInfo(); extrainfo+=wifiinfo.getBSSID(); subtype += wifiinfo.getNetworkId(); }*/ netstatestring = String.format("%2$s %4$s to %1$s %3$s", networkInfo.getTypeName(), networkInfo.getDetailedState(), extrainfo, subtype); } if (networkInfo != null && networkInfo.getState() == State.CONNECTED) { int newnet = networkInfo.getType(); boolean pendingDisconnect = (network == connectState.PENDINGDISCONNECT); network = connectState.SHOULDBECONNECTED; boolean sameNetwork; if (lastConnectedNetwork == null || lastConnectedNetwork.getType() != networkInfo.getType() || !equalsObj(lastConnectedNetwork.getExtraInfo(), networkInfo.getExtraInfo()) ) sameNetwork = false; else sameNetwork = true; /* Same network, connection still 'established' */ if (pendingDisconnect && sameNetwork) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); // Reprotect the sockets just be sure mManagement.networkChange(true); } else { /* Different network or connection not established anymore */ if (screen == connectState.PENDINGDISCONNECT) screen = connectState.DISCONNECTED; if (shouldBeConnected()) { mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable); if (pendingDisconnect || !sameNetwork) mManagement.networkChange(sameNetwork); else mManagement.resume(); } lastNetwork = newnet; lastConnectedNetwork = networkInfo; } } else if (networkInfo == null) { // Not connected, stop openvpn, set last connected network to no network lastNetwork = -1; if (sendusr1) { network = connectState.PENDINGDISCONNECT; mDisconnectHandler.postDelayed(mDelayDisconnectRunnable, DISCONNECT_WAIT * 1000); } } if (!netstatestring.equals(lastStateMsg)) VpnStatus.logInfo(R.string.netstatus, netstatestring); lastStateMsg = netstatestring; }
Example 20
Source File: NetworkStateReceiver.java From FimiX8-RE with MIT License | 4 votes |
private boolean isNetworkConnected(State state) { if (state == null || State.CONNECTED != state) { return false; } return true; }