Java Code Examples for android.net.NetworkInfo#isConnectedOrConnecting()
The following examples show how to use
android.net.NetworkInfo#isConnectedOrConnecting() .
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: Service.java From Twire with GNU General Public License v3.0 | 6 votes |
/** * Checks if the device is connected to a valid network * Can only be called on a thread */ public static boolean isNetworkConnectedThreadOnly(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnectedOrConnecting()) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("https://clients3.google.com/generate_204") .openConnection()); urlc.setRequestProperty("User-Agent", "Android"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0); } catch (IOException e) { Log.e("SERVICE", "Error checking internet connection", e); } } else { Log.d("SERVICE", "No network available!"); } return false; }
Example 2
Source File: Util.java From droidddle with Apache License 2.0 | 6 votes |
public static boolean isConnected(Context context) { if (context == null) return false; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null) return false; return ((activeNetworkInfo != null) && (activeNetworkInfo .isConnectedOrConnecting())); } return false; }
Example 3
Source File: CheckNetworkConnection.java From PhotoDiscovery with Apache License 2.0 | 5 votes |
public static boolean isConnectionAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected() && netInfo.isConnectedOrConnecting() && netInfo.isAvailable()) { return true; } } return false; }
Example 4
Source File: NavigationViewModel.java From graphhopper-navigation-android with MIT License | 5 votes |
@SuppressWarnings( {"MissingPermission"}) private boolean hasNetworkConnection() { if (connectivityManager == null) { return false; } NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); }
Example 5
Source File: OfflinePageUtils.java From AndroidChromium with Apache License 2.0 | 5 votes |
/** Returns the current device conditions. May be overridden for testing. */ protected DeviceConditions getDeviceConditionsImpl(Context context) { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); // Note this is a sticky intent, so we aren't really registering a receiver, just getting // the sticky intent. That means that we don't need to unregister the filter later. Intent batteryStatus = context.registerReceiver(null, filter); if (batteryStatus == null) return null; // Get the connection type from chromium's internal object. int connectionType = NetworkChangeNotifier.getInstance().getCurrentConnectionType(); // Sometimes the NetworkConnectionNotifier lags the actual connection type, especially when // the GCM NM wakes us from doze state. If we are really connected, report the connection // type from android. if (connectionType == ConnectionType.CONNECTION_NONE) { // Get the connection type from android in case chromium's type is not yet set. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if (isConnected) { connectionType = convertAndroidNetworkTypeToConnectionType(activeNetwork.getType()); } } return new DeviceConditions( isPowerConnected(batteryStatus), batteryPercentage(batteryStatus), connectionType); }
Example 6
Source File: Utils.java From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static boolean isInternetAvailable(Context context) { ConnectivityManager cm = (ConnectivityManager) ApplozicService.getContext(context).getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return (activeNetwork != null && activeNetwork.isConnectedOrConnecting()); }
Example 7
Source File: OTADownload.java From product-emm with Apache License 2.0 | 5 votes |
public boolean checkNetworkOnline() { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectivityManager.getActiveNetworkInfo(); boolean status = false; if (info != null && info.isConnectedOrConnecting()) { status = true; } return status; }
Example 8
Source File: ImageFetcher.java From vocefiscal-android with Apache License 2.0 | 5 votes |
/** * Simple network connection check. * * @param context */ private void checkConnection(Context context) { final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { //Toast.makeText(context, R.string.no_network_connection_toast, Toast.LENGTH_LONG).show(); //Log.e(TAG, "checkConnection - no connection found"); } }
Example 9
Source File: NetworkConnectionUtil.java From firebase-chat with MIT License | 5 votes |
/** * Check if the device is connected to internet via <i>wifi</i> or not. * * @param context Current context of the application * @return <b>true</b> if device is connected to internet via <i>wifi</i>, otherwise <b>false</b> */ public static boolean isConnectedToWifi(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI; }
Example 10
Source File: MainActivity.java From Chorus-RF-Laptimer with MIT License | 5 votes |
private boolean checkIsWifiOnAndConnected() { WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); if (!wifiMgr.isWifiEnabled()) return false; // Wi-Fi adapter is OFF ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); // Deprecated in Android 10, but should still be fine NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); return isConnected; }
Example 11
Source File: ConnectivityHelper.java From AndroidCommons with Apache License 2.0 | 5 votes |
@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE) public static boolean isConnected(Context context) { final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo activeNetwork = manager.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); }
Example 12
Source File: CommonUtils.java From letv with Apache License 2.0 | 5 votes |
public static boolean canTryConnection(Context context) { if (!checkPermission(context, "android.permission.ACCESS_NETWORK_STATE")) { return true; } NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo(); if (activeNetwork == null || !activeNetwork.isConnectedOrConnecting()) { return false; } return true; }
Example 13
Source File: DatapointHelper.java From Beats with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Determines the type of network this device is connected to. * * @param context the context used to access the device's WIFI * @param telephonyManager The manager used to access telephony info * @return The type of network, or unknown if the information is unavailable */ public static String getNetworkType(final Context context, final TelephonyManager telephonyManager) { try { if (context.getPackageManager().checkPermission(permission.ACCESS_WIFI_STATE, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) { final NetworkInfo wifiInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (null != wifiInfo && wifiInfo.isConnectedOrConnecting()) { return "wifi"; //$NON-NLS-1$ } } else { if (Constants.IS_LOGGABLE) { Log.w(Constants.LOG_TAG, "Application does not have one more more of the following permissions: ACCESS_WIFI_STATE. Determining Wi-Fi connectivity is unavailable"); //$NON-NLS-1$ } } } catch (final SecurityException e) { /* * Although the documentation doesn't declare it, sometimes the ConnectivityService will throw an exception for * permission ACCESS_NETWORK_STATE */ if (Constants.IS_LOGGABLE) { Log.w(Constants.LOG_TAG, "Application does not have the permission ACCESS_NETWORK_STATE. Determining Wi-Fi connectivity is unavailable", e); //$NON-NLS-1$ } } return "android_network_type_" + telephonyManager.getNetworkType(); //$NON-NLS-1$ }
Example 14
Source File: TweetFragment.java From catnut with MIT License | 4 votes |
public boolean isNetworkAvailable() { NetworkInfo activeNetwork = mConnectivityManager.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); }
Example 15
Source File: DeviceUtils.java From AndroidWear-OpenWear with MIT License | 4 votes |
public static NetworkType getNetworkType(Context context) { NetworkType type = NetworkType.NETWORK_UNKNOWN; ConnectivityManager mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWifi != null && mWifi.isAvailable() && mWifi.isConnectedOrConnecting()) { type = NetworkType.NETWORK_WIFI; } else { NetworkInfo mMobile = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobile != null && mMobile.isAvailable() && mMobile.isConnectedOrConnecting()) { int subType = mMobile.getSubtype(); switch (subType) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: type = NetworkType.NETWORK_2_G; break; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: type = NetworkType.NETWORK_3_G; break; case TelephonyManager.NETWORK_TYPE_LTE: type = NetworkType.NETWORK_4_G; break; } } else { type = NetworkType.NO_NETWORK; } } return type; }
Example 16
Source File: PlayPianoPresenter.java From android-things-distributed-piano with Apache License 2.0 | 4 votes |
private boolean isConnectedToNetwork() { ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo info1 = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET); return (info != null && info.isConnectedOrConnecting()) || (info1 != null && info1.isConnectedOrConnecting()); }
Example 17
Source File: NetworkUtils.java From BuildmLearn-Toolkit-Android with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static boolean isNetworkAvailable(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting(); }
Example 18
Source File: NetUtil.java From AndroidStudyDemo with GNU General Public License v2.0 | 4 votes |
/** * 判断当前是否网络连接 * * @param context 上下文 * @return 状态码 */ public NetState isConnected(Context context) { NetState stateCode = NetState.NET_NO; ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { switch (ni.getType()) { case ConnectivityManager.TYPE_WIFI: stateCode = NetState.NET_WIFI; break; case ConnectivityManager.TYPE_MOBILE: switch (ni.getSubtype()) { case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: stateCode = NetState.NET_2G; break; case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: stateCode = NetState.NET_3G; break; case TelephonyManager.NETWORK_TYPE_LTE: stateCode = NetState.NET_4G; break; default: stateCode = NetState.NET_UNKNOWN; } break; default: stateCode = NetState.NET_UNKNOWN; } } return stateCode; }
Example 19
Source File: ConnectivityReceiver.java From Zom-Android-XMPP with GNU General Public License v3.0 | 4 votes |
public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); }
Example 20
Source File: AppUtils.java From materialistic with Apache License 2.0 | 4 votes |
public static boolean hasConnection(Context context) { NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting(); }