Class ConnectivityMonitor


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

      Fields 
      Modifier and Type Field Description
      static int[] ALL_NETWORKS
      An array of all supported network types.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ALL_NETWORKS

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

      • 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 java.lang.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<java.lang.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<java.lang.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.