Java Code Examples for android.net.NetworkInfo#isConnected()
The following examples show how to use
android.net.NetworkInfo#isConnected() .
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: AutoUpdateApk.java From android-auto-updater-client with Apache License 2.0 | 6 votes |
@Override public void onReceive(Context context, Intent intent) { NetworkInfo currentNetworkInfo = (NetworkInfo) intent .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); // do application-specific task(s) based on the current network // state, such // as enabling queuing of HTTP requests when currentNetworkInfo is // connected etc. boolean not_mobile = currentNetworkInfo.getTypeName() .equalsIgnoreCase("MOBILE") ? false : true; if (currentNetworkInfo.isConnected() && (mobile_updates || not_mobile)) { checkUpdates(false); updateHandler.postDelayed(periodicUpdate, updateInterval); } else { updateHandler.removeCallbacks(periodicUpdate); // no network // anyway } }
Example 2
Source File: NetworkChangeNotifierAutoDetect.java From 365browser with Apache License 2.0 | 6 votes |
/** * Returns connection type for |network|. * Only callable on Lollipop and newer releases. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) @ConnectionType int getConnectionType(Network network) { NetworkInfo networkInfo = getNetworkInfo(network); if (networkInfo != null && networkInfo.getType() == TYPE_VPN) { // When a VPN is in place the underlying network type can be queried via // getActiveNeworkInfo() thanks to // https://android.googlesource.com/platform/frameworks/base/+/d6a7980d networkInfo = mConnectivityManager.getActiveNetworkInfo(); } if (networkInfo != null && networkInfo.isConnected()) { return convertToConnectionType(networkInfo.getType(), networkInfo.getSubtype()); } return ConnectionType.CONNECTION_NONE; }
Example 3
Source File: AirplaneModeAndroidTest.java From firebase-jobdispatcher-android with Apache License 2.0 | 6 votes |
private void waitForSomeNetworkToConnect() throws Exception { final SettableFuture<Void> future = SettableFuture.create(); ConnectivityManager.NetworkCallback cb = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { NetworkInfo netInfo = connManager.getNetworkInfo(network); if (netInfo != null && netInfo.isConnected()) { future.set(null); } } }; connManager.requestNetwork( new NetworkRequest.Builder().addCapability(NET_CAPABILITY_INTERNET).build(), cb); try { future.get(NETWORK_STATE_CHANGE_TIMEOUT_SECONDS, TimeUnit.SECONDS); } finally { connManager.unregisterNetworkCallback(cb); } }
Example 4
Source File: ConnectivityMonitor.java From unity-ads-android with Apache License 2.0 | 6 votes |
private static void initConnectionStatus() { ConnectivityManager cm = (ConnectivityManager)ClientProperties.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); if(cm == null) { return; } NetworkInfo ni = cm.getActiveNetworkInfo(); if(ni != null && ni.isConnected()) { _connected = 1; _wifi = ni.getType() == ConnectivityManager.TYPE_WIFI; if(!_wifi) { TelephonyManager tm = (TelephonyManager)ClientProperties.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); _networkType = tm.getNetworkType(); } } else { _connected = 0; } }
Example 5
Source File: NetworkStauts.java From WebViewJavaScriptBridge with Apache License 2.0 | 6 votes |
private static int getNetworkClass(Context context) { int networkType = NETWORK_TYPE_UNKNOWN; try { final NetworkInfo network = ((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (network != null && network.isAvailable() && network.isConnected()) { int type = network.getType(); if (type == ConnectivityManager.TYPE_WIFI) { networkType = NETWORK_TYPE_WIFI; } else if (type == ConnectivityManager.TYPE_MOBILE) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); networkType = telephonyManager.getNetworkType(); } } else { networkType = NETWORK_TYPE_UNAVAILABLE; } } catch (Exception ex) { ex.printStackTrace(); } return getNetworkClassByType(networkType); }
Example 6
Source File: NetworkUtils.java From seny-devpkg with Apache License 2.0 | 6 votes |
/** * Get network type name * * @param context * @return */ public static String getNetworkTypeName(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; String type = NETWORK_TYPE_DISCONNECT; if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) { return type; } ; if (networkInfo.isConnected()) { String typeName = networkInfo.getTypeName(); if ("WIFI".equalsIgnoreCase(typeName)) { type = NETWORK_TYPE_WIFI; } else if ("MOBILE".equalsIgnoreCase(typeName)) { String proxyHost = android.net.Proxy.getDefaultHost(); type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP; } else { type = NETWORK_TYPE_UNKNOWN; } } return type; }
Example 7
Source File: Preferences.java From candybar-library with Apache License 2.0 | 5 votes |
public boolean isConnectedToNetwork() { try { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } catch (Exception e) { return false; } }
Example 8
Source File: Sentry.java From Sentry-Android with MIT License | 5 votes |
private boolean shouldAttemptPost() { PackageManager pm = context.getPackageManager(); int hasPerm = pm.checkPermission(permission.ACCESS_NETWORK_STATE, context.getPackageName()); if (hasPerm != PackageManager.PERMISSION_GRANTED) { return false; } ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Example 9
Source File: NetworkChangeNotifierAutoDetect.java From 365browser with Apache License 2.0 | 5 votes |
/** * @return the info of the network that is available to this app. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) private NetworkInfo getActiveNetworkInfo() { final NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { return null; } if (networkInfo.isConnected()) { return networkInfo; } // If |networkInfo| is BLOCKED, but the app is in the foreground, then it's likely that // Android hasn't finished updating the network access permissions as BLOCKED is only // meant for apps in the background. See https://crbug.com/677365 for more details. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // https://crbug.com/677365 primarily affects only Lollipop and higher versions. return null; } if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOCKED) { // Network state is not blocked which implies that network access is // unavailable (not just blocked to this app). return null; } if (ApplicationStatus.getStateForApplication() != ApplicationState.HAS_RUNNING_ACTIVITIES) { // The app is not in the foreground. return null; } return networkInfo; }
Example 10
Source File: Preferences.java From candybar with Apache License 2.0 | 5 votes |
public boolean isConnectedAsPreferred() { try { if (isWifiOnly()) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI && activeNetworkInfo.isConnected(); } return true; } catch (Exception e) { return false; } }
Example 11
Source File: JCUtils.java From JCVideoPlayer with MIT License | 5 votes |
public static boolean isWifiConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetworkInfo.isConnected()) { return true; } return false; }
Example 12
Source File: NetWorkUtil.java From JianDanRxJava with Apache License 2.0 | 5 votes |
/** * 判断当前的网络连接方式是否为WIFI * * @param context * @return */ public static boolean isWifiConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); return wifiNetworkInfo.isConnected(); }
Example 13
Source File: NetworkStateReceiver.java From bridgefy-android-samples with MIT License | 5 votes |
@Override public void onReceive(Context context, Intent intent) { NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (info != null && info.isConnected()) { Log.i(TAG, "isConnected!"); LocalBroadcastManager.getInstance(context).sendBroadcast( new Intent(WIFI_STATE_CONNECTED)); } }
Example 14
Source File: AptoideUtils.java From aptoide-client with GNU General Public License v2.0 | 4 votes |
public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Example 15
Source File: MainActivity.java From Zom-Android-XMPP with GNU General Public License v3.0 | 4 votes |
private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Example 16
Source File: SalutBroadcastReceiver.java From Salut with MIT License | 4 votes |
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state != WifiP2pManager.WIFI_P2P_STATE_ENABLED) { Log.v(TAG, " WiFi P2P is no longer enabled."); } } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { if (manager == null) { return; } NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected() && networkInfo.getTypeName().equals("WIFI_P2P")) { salutInstance.isConnectedToAnotherDevice = true; manager.requestConnectionInfo(channel, salutInstance); } else { salutInstance.isConnectedToAnotherDevice = false; Log.v(TAG, "Not connected to another device."); if (salutInstance.thisDevice.isRegistered) { if (salutInstance.unexpectedDisconnect != null) { salutInstance.unregisterClient(salutInstance.unexpectedDisconnect, null, false); } } } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); if (salutInstance.thisDevice.deviceName == null) { salutInstance.thisDevice.deviceName = device.deviceName; salutInstance.thisDevice.macAddress = device.deviceAddress; } } }
Example 17
Source File: AddReviewActivity.java From CineLog with GNU General Public License v3.0 | 4 votes |
private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Example 18
Source File: JsonObjectGetter.java From HouseAds2 with MIT License | 4 votes |
private boolean isNetworkAvailable(Context ctx) { ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Example 19
Source File: NetUtil.java From NiceRead with Apache License 2.0 | 2 votes |
/** * 判断wifi是否连接 * * @param context * @return */ public static boolean isWifiConnected(Context context) { NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo(); return net != null && net.getType() == ConnectivityManager.TYPE_WIFI && net.isConnected(); }
Example 20
Source File: AbstractNetworkUtils.java From tenor-android-core with Apache License 2.0 | 2 votes |
/** * Checks if device is connected to a cellular network * * @param context the context * @return true if connected */ public static boolean isCellularConnected(@NonNull Context context) { NetworkInfo info = getNetworkInfo(context); return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE); }