Package android.net
Class ConnectivityManager
- java.lang.Object
-
- android.net.ConnectivityManager
-
- Direct Known Subclasses:
PollerConnectivityManager
public class ConnectivityManager extends Object
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance is dependent on the underlying system.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConnectivityManager.NetworkCallbackBase class for NetworkRequest callbacks.
-
Constructor Summary
Constructors Modifier Constructor Description protectedConnectivityManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoNotifyAvailabilityChange(Network network, boolean available, ConnectivityManager.NetworkCallback[] networkCallbacks)Notify the networkCallbacks about an availability change.protected voiddoNotifyCapabilitiesChange(Network network, NetworkCapabilities capabilities, ConnectivityManager.NetworkCallback[] networkCallBacks)Notify the callback of a capabilities change.NetworkgetActiveNetwork()Returns aNetworkobject corresponding to the currently active default data network.NetworkInfogetActiveNetworkInfo()Returns details about the currently active default data network.Network[]getAllNetworks()Returns an array of allNetworkcurrently tracked by the framework.NetworkCapabilitiesgetNetworkCapabilities(Network network)Get theNetworkCapabilitiesfor the given Network.NetworkInfogetNetworkInfo(Network network)Returns connection status information about a particular Network.protected voidnotifyNetworkCallbacks(Network network, boolean available)Notifies a network availability change.protected voidnotifyNetworkCallbacks(Network network, NetworkCapabilities capabilities)Notifies a network capability change.voidregisterDefaultNetworkCallback(ConnectivityManager.NetworkCallback networkCallback)Registers to receive notifications about changes in the system default network.voidregisterNetworkCallback(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback)Registers to receive notifications about all networks which satisfy the givenNetworkRequest.protected voidsetActiveNetwork(Network network)Sets the active network.protected voidsetAvailable(boolean available, Network network, NetworkInfo networkInfo)Sets the network availability.voidunregisterNetworkCallback(ConnectivityManager.NetworkCallback networkCallback)Unregisters callbacks about and possibly releases networks originating fromregisterNetworkCallback(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback)calls.
-
-
-
Method Detail
-
getActiveNetworkInfo
@Nullable public NetworkInfo getActiveNetworkInfo()
Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always checkNetworkInfo.isConnected()before initiating network traffic. This may returnnullwhen there is no default network.- Returns:
- a
NetworkInfoobject for the current default network ornullif no network default network is currently active
-
getActiveNetwork
@Nullable public Network getActiveNetwork()
Returns aNetworkobject corresponding to the currently active default data network. In the event that the current active default data network disconnects, the returnedNetworkobject will no longer be usable. This will return null when there is no default network.- Returns:
- a
Networkobject for the current default network ornullif no default network is currently active
-
getAllNetworks
public Network[] getAllNetworks()
Returns an array of allNetworkcurrently tracked by the framework.- Returns:
- an array of
Networkobjects.
-
getNetworkCapabilities
@Nullable public NetworkCapabilities getNetworkCapabilities(Network network)
Get theNetworkCapabilitiesfor the given Network. This will returnnullif the network is unknown.- Parameters:
network- TheNetworkobject identifying the network in question.- Returns:
- The
NetworkCapabilitiesfor the network, ornull.
-
getNetworkInfo
@Nullable public NetworkInfo getNetworkInfo(Network network)
Returns connection status information about a particular Network.- Parameters:
network-Networkspecifying which network in which you're interested.- Returns:
- a
NetworkInfoobject for the requested network ornullif the Network is not valid.
-
registerDefaultNetworkCallback
public void registerDefaultNetworkCallback(ConnectivityManager.NetworkCallback networkCallback)
Registers to receive notifications about changes in the system default network. The callbacks will continue to be called until either the application exits orunregisterNetworkCallback(NetworkCallback)is called.- Parameters:
networkCallback- TheConnectivityManager.NetworkCallbackthat the system will call as the system default network changes. The callback is invoked on the default internal Handler.
-
registerNetworkCallback
public void registerNetworkCallback(@Nullable NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback)
Registers to receive notifications about all networks which satisfy the givenNetworkRequest. The callbacks will continue to be called until either the application exits orunregisterNetworkCallback(android.net.ConnectivityManager.NetworkCallback)is called- Parameters:
request-NetworkRequestdescribing this request.networkCallback- TheConnectivityManager.NetworkCallbackthat the system will call as suitable networks change state.
-
unregisterNetworkCallback
public void unregisterNetworkCallback(ConnectivityManager.NetworkCallback networkCallback)
Unregisters callbacks about and possibly releases networks originating fromregisterNetworkCallback(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback)calls. If the givenNetworkCallbackhad previosuly been used with#requestNetwork, any networks that had been connected to only to satisfy that request will be disconnected.- Parameters:
networkCallback- TheConnectivityManager.NetworkCallbackused when making the request.
-
setActiveNetwork
protected void setActiveNetwork(Network network)
Sets the active network.- Parameters:
network- the network to set.
-
notifyNetworkCallbacks
protected void notifyNetworkCallbacks(Network network, boolean available)
Notifies a network availability change.- Parameters:
network- the network.available-trueif the network is available.
-
notifyNetworkCallbacks
protected void notifyNetworkCallbacks(Network network, NetworkCapabilities capabilities)
Notifies a network capability change.- Parameters:
network- the network.capabilities- the network capabilities.
-
setAvailable
protected void setAvailable(boolean available, @Nullable Network network, NetworkInfo networkInfo)Sets the network availability.- Parameters:
available-trueif available.network- the network.networkInfo- the network info.
-
doNotifyCapabilitiesChange
protected void doNotifyCapabilitiesChange(Network network, NetworkCapabilities capabilities, ConnectivityManager.NetworkCallback[] networkCallBacks)
Notify the callback of a capabilities change.- Parameters:
network- the network.capabilities- the capabilities.networkCallBacks- the callback to call.
-
doNotifyAvailabilityChange
protected void doNotifyAvailabilityChange(Network network, boolean available, ConnectivityManager.NetworkCallback[] networkCallbacks)
Notify the networkCallbacks about an availability change.- Parameters:
network- the network.available- the availability.networkCallbacks- the callback to call.
-
-