Class ConnectivityMonitor

java.lang.Object
com.brightcove.player.network.ConnectivityMonitor

public class ConnectivityMonitor extends Object
Connectivity monitor can be used to monitor changes to host network connectivity changes.
  • Field Details

    • ALL_NETWORKS

      public static final int[] ALL_NETWORKS
      An array of all supported network types.
  • Method Details

    • getInstance

      public static ConnectivityMonitor getInstance(@NonNull android.content.Context context)
      Get reference to a cached instance of ConnectivityMonitor.
      Parameters:
      context - the android context.
      Returns:
      reference to the connectivity monitor.
    • addListener

      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.

      Parameters:
      listener - reference to the listener.
      Returns:
      reference to the single instance of ConnectivityMonitor
    • removeListener

      public ConnectivityMonitor removeListener(@NonNull 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.
      Parameters:
      listener - reference to the listener.
      Returns:
      reference to the single instance of ConnectivityMonitor
    • isConnected

      public boolean isConnected()
      Checks if the host device has an active network connection.
      Returns:
      true if the host has network connection, otherwise false.
    • getActiveNetworkName

      @NonNull public String getActiveNetworkName()
      Gets the human readable name of the current network, if connected.
      Returns:
      empty string if the host is not connected, otherwise the network type name.
    • isConnected

      public boolean isConnected(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.
      Returns:
      true if the host is connecting using an allowed network type, otherwise false.
    • isConnected

      public boolean isConnected(@NonNull android.util.SparseArray<Boolean> networkEntitlements)
      Checks if the host device is connected to the using one of the allowed network type.
      Parameters:
      networkEntitlements - a SparseArray of ConnectivityMonitor.NetworkType and Boolean values that indicate whether a specific network type can be used.
      Returns:
      true if the host is connecting using an allowed network type, otherwise false.
    • isConnected

      public static boolean isConnected(@Nullable android.net.NetworkInfo networkInfo, @NonNull android.util.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 ConnectivityMonitor.NetworkType and Boolean values that indicate whether a specific network type can be used.
      Returns:
      true if the host is connecting using an allowed network type, otherwise false.
    • isWifiConnection

      public boolean isWifiConnection()
      Checks if the active network connection is a Wi-Fi network.
      Returns:
      true if the active connection is a Wi-Fi network, otherwise false.