android.net.ConnectivityManager.NetworkCallback Java Examples
The following examples show how to use
android.net.ConnectivityManager.NetworkCallback.
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: WifiUtil.java From android-wificonnect with MIT License | 7 votes |
@TargetApi(LOLLIPOP) NetworkCallback networkCallback(final String networkSSID, final NetworkStateChangeListener listener) { return new NetworkCallback() { @Override public void onAvailable(Network network) { NetworkInfo networkInfo = manager.getNetworkInfo(network); logger.i("Network is Available. Network Info: " + networkInfo); if (WifiHelper.areEqual(networkInfo.getExtraInfo(), networkSSID)) { manager.unregisterNetworkCallback(this); networkCallback = null; bindToRequiredNetwork(network); logger.i(format("Bound application to use %s network", networkSSID)); listener.onNetworkBound(); } } }; }
Example #2
Source File: SambaProviderApplication.java From samba-documents-provider with GNU General Public License v3.0 | 6 votes |
private void registerNetworkCallback(Context context) { final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); manager.registerNetworkCallback( new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET) .build(), new NetworkCallback() { @Override public void onAvailable(Network network) { mSambaClient.reset(); } }); }
Example #3
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 5 votes |
/** Only callable on Lollipop and newer releases. */ @SuppressLint("NewApi") public void registerNetworkCallback(NetworkCallback networkCallback) { connectivityManager.registerNetworkCallback( new NetworkRequest.Builder() .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .build(), networkCallback); }
Example #4
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 5 votes |
/** Only callable on Lollipop and newer releases. */ @SuppressLint("NewApi") public void requestMobileNetwork(NetworkCallback networkCallback) { NetworkRequest.Builder builder = new NetworkRequest.Builder(); builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); connectivityManager.requestNetwork(builder.build(), networkCallback); }
Example #5
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 5 votes |
@SuppressLint("NewApi") public void releaseCallback(NetworkCallback networkCallback) { if (supportNetworkCallback()) { Logging.d(TAG, "Unregister network callback"); connectivityManager.unregisterNetworkCallback(networkCallback); } }
Example #6
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 5 votes |
/** * Constructs a NetworkMonitorAutoDetect. Should only be called on UI thread. */ @SuppressLint("NewApi") public NetworkMonitorAutoDetect(Observer observer, Context context) { this.observer = observer; this.context = context; connectivityManagerDelegate = new ConnectivityManagerDelegate(context); wifiManagerDelegate = new WifiManagerDelegate(context); final NetworkState networkState = connectivityManagerDelegate.getNetworkState(); connectionType = getConnectionType(networkState); wifiSSID = getWifiSSID(networkState); intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); if (PeerConnectionFactory.fieldTrialsFindFullName("IncludeWifiDirect").equals("Enabled")) { wifiDirectManagerDelegate = new WifiDirectManagerDelegate(observer, context); } registerReceiver(); if (connectivityManagerDelegate.supportNetworkCallback()) { // On Android 6.0.0, the WRITE_SETTINGS permission is necessary for // requestNetwork, so it will fail. This was fixed in Android 6.0.1. NetworkCallback tempNetworkCallback = new NetworkCallback(); try { connectivityManagerDelegate.requestMobileNetwork(tempNetworkCallback); } catch (java.lang.SecurityException e) { Logging.w(TAG, "Unable to obtain permission to request a cellular network."); tempNetworkCallback = null; } mobileNetworkCallback = tempNetworkCallback; allNetworkCallback = new SimpleNetworkCallback(); connectivityManagerDelegate.registerNetworkCallback(allNetworkCallback); } else { mobileNetworkCallback = null; allNetworkCallback = null; } }
Example #7
Source File: MultipathPolicyTracker.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private void registerTrackMobileCallback() { final NetworkRequest request = new NetworkRequest.Builder() .addCapability(NET_CAPABILITY_INTERNET) .addTransportType(TRANSPORT_CELLULAR) .build(); mMobileNetworkCallback = new ConnectivityManager.NetworkCallback() { @Override public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { MultipathTracker existing = mMultipathTrackers.get(network); if (existing != null) { existing.setNetworkCapabilities(nc); existing.updateMultipathBudget(); return; } try { mMultipathTrackers.put(network, new MultipathTracker(network, nc)); } catch (IllegalStateException e) { Slog.e(TAG, "Can't track mobile network " + network + ": " + e.getMessage()); } if (DBG) Slog.d(TAG, "Tracking mobile network " + network); } @Override public void onLost(Network network) { MultipathTracker existing = mMultipathTrackers.get(network); if (existing != null) { existing.shutdown(); mMultipathTrackers.remove(network); } if (DBG) Slog.d(TAG, "No longer tracking mobile network " + network); } }; mCM.registerNetworkCallback(request, mMobileNetworkCallback, mHandler); }
Example #8
Source File: NetworkChangeNotifierAutoDetect.java From cronet with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Registers networkCallback to receive notifications about networks * that satisfy networkRequest. * Only callable on Lollipop and newer releases. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) void registerNetworkCallback( NetworkRequest networkRequest, NetworkCallback networkCallback, Handler handler) { // Starting with Oreo specifying a Handler is allowed. Use this to avoid thread-hops. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mConnectivityManager.registerNetworkCallback( networkRequest, networkCallback, handler); } else { mConnectivityManager.registerNetworkCallback(networkRequest, networkCallback); } }
Example #9
Source File: UpstreamNetworkMonitor.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
private void releaseCallback(NetworkCallback cb) { if (cb != null) cm().unregisterNetworkCallback(cb); }
Example #10
Source File: NetworkChangeNotifierAutoDetect.java From cronet with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Registers networkCallback to receive notifications about default network. * Only callable on P and newer releases. */ @TargetApi(Build.VERSION_CODES.P) void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) { mConnectivityManager.registerDefaultNetworkCallback(networkCallback, handler); }
Example #11
Source File: NetworkChangeNotifierAutoDetect.java From cronet with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Unregisters networkCallback from receiving notifications. * Only callable on Lollipop and newer releases. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) void unregisterNetworkCallback(NetworkCallback networkCallback) { mConnectivityManager.unregisterNetworkCallback(networkCallback); }
Example #12
Source File: NetworkChangeNotifierAutoDetect.java From 365browser with Apache License 2.0 | 4 votes |
/** * Registers networkCallback to receive notifications about networks * that satisfy networkRequest. * Only callable on Lollipop and newer releases. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) void registerNetworkCallback( NetworkRequest networkRequest, NetworkCallback networkCallback) { mConnectivityManager.registerNetworkCallback(networkRequest, networkCallback); }
Example #13
Source File: NetworkChangeNotifierAutoDetect.java From 365browser with Apache License 2.0 | 4 votes |
/** * Unregisters networkCallback from receiving notifications. * Only callable on Lollipop and newer releases. */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) void unregisterNetworkCallback(NetworkCallback networkCallback) { mConnectivityManager.unregisterNetworkCallback(networkCallback); }