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