Package 

Class NetworkStateProvider


  • 
    public final class NetworkStateProvider
    
                        

    Provides network connectivity state monitoring and change notifications.

    This class monitors the device's network connectivity state and notifies registered listeners when the connection state changes. It uses Android's ConnectivityManager to track network availability and validates that networks have internet access capabilities.

    The provider automatically starts monitoring when the first listener is subscribed and stops when the last listener is unsubscribed to optimize battery usage.

    • Constructor Detail

      • NetworkStateProvider

        NetworkStateProvider(CoroutineScope scope, ConnectivityManager connectivityManager)
        Parameters:
        scope - The CoroutineScope in which network state changes will be handled
        connectivityManager - The system's ConnectivityManager service
    • Method Detail

      • isConnected

         final Boolean isConnected()

        Checks if the device is currently connected to a network with internet access.

        For API level 23 (Android M) and above, this method validates that the network has both internet capability and is validated by the system. For older versions, it falls back to checking if the active network info indicates a connection.

      • subscribe

         final Unit subscribe(NetworkStateProvider.NetworkStateListener listener)

        Subscribes to network state changes.

        When the first listener is subscribed, the provider automatically starts monitoring network changes. The listener will be notified immediately if there are any subsequent changes in network connectivity.

        Parameters:
        listener - The listener to be notified about network state changes
      • unsubscribe

         final Unit unsubscribe(NetworkStateProvider.NetworkStateListener listener)

        Unsubscribes from network state changes.

        When the last listener is unsubscribed, the provider automatically stops monitoring network changes to conserve battery and system resources.

        Parameters:
        listener - The listener to be removed