-
public class ConnectivityMonitor
Connectivity monitor can be used to monitor changes to host network connectivity changes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public @interface
ConnectivityMonitor.NetworkType
List of supported network types.
public interface
ConnectivityMonitor.Listener
The contract of a listener that can receive callback notifications from the ConnectivityMonitor
-
Field Summary
Fields Modifier and Type Field Description public final static Array<int>
ALL_NETWORKS
-
Method Summary
Modifier and Type Method Description static ConnectivityMonitor
getInstance(@NonNull() Context context)
Get reference to a cached instance of ConnectivityMonitor. ConnectivityMonitor
addListener(@NonNull() ConnectivityMonitor.Listener listener)
Adds the specified listener to the list of listeners that will be notified when thehost device's network connectivity state changes. ConnectivityMonitor
removeListener(@NonNull() ConnectivityMonitor.Listener listener)
Removes the specified listener from the list of listeners that will be notified when thehost device's network connectivity state changes. boolean
isConnected()
Checks if the host device has an active network connection. String
getActiveNetworkName()
Gets the human readable name of the current network, if connected. boolean
isConnected(Array<int> allowedTypes)
Checks if the host device is connected to the using one of the allowed network type. boolean
isConnected(@NonNull() SparseArray<Boolean> networkEntitlements)
Checks if the host device is connected to the using one of the allowed network type. static boolean
isConnected(@Nullable() NetworkInfo networkInfo, @NonNull() SparseArray<Boolean> networkEntitlements)
Checks if the specified network is one of the allowed network type. boolean
isWifiConnection()
Checks if the active network connection is a Wi-Fi network. -
-
Method Detail
-
getInstance
static ConnectivityMonitor getInstance(@NonNull() Context context)
Get reference to a cached instance of ConnectivityMonitor.
- Parameters:
context
- the android context.
-
addListener
ConnectivityMonitor addListener(@NonNull() ConnectivityMonitor.Listener listener)
Adds the specified listener to the list of listeners that will be notified when thehost device's network connectivity state changes. Please call removeListener to free up resources.
Please note that the design of the connectivity monitor intentionally discouragesthe practice of using anonymous objects as listeners to avoid resource leaks. The listenerwill be held weakly by this class. Anonymous listeners may get collected by GC immediately.
- Parameters:
listener
- reference to the listener.
-
removeListener
ConnectivityMonitor removeListener(@NonNull() ConnectivityMonitor.Listener listener)
Removes the specified listener from the list of listeners that will be notified when thehost device's network connectivity state changes.
- Parameters:
listener
- reference to the listener.
-
isConnected
boolean isConnected()
Checks if the host device has an active network connection.
-
getActiveNetworkName
@NonNull() String getActiveNetworkName()
Gets the human readable name of the current network, if connected.
-
isConnected
boolean isConnected(Array<int> allowedTypes)
Checks if the host device is connected to the using one of the allowed network type.
- Parameters:
allowedTypes
- array of allowed network types.
-
isConnected
boolean isConnected(@NonNull() SparseArray<Boolean> networkEntitlements)
Checks if the host device is connected to the using one of the allowed network type.
- Parameters:
networkEntitlements
- a SparseArray of NetworkType and Boolean values that indicatewhether a specific network type can be used.
-
isConnected
static boolean isConnected(@Nullable() NetworkInfo networkInfo, @NonNull() SparseArray<Boolean> networkEntitlements)
Checks if the specified network is one of the allowed network type.
- Parameters:
networkInfo
- the current network information.networkEntitlements
- a SparseArray of NetworkType and Boolean values that indicatewhether a specific network type can be used.
-
isWifiConnection
boolean isWifiConnection()
Checks if the active network connection is a Wi-Fi network.
-
-
-
-