Interface NetworkMonitor

All Superinterfaces:
Initable, Proxy
All Known Implementing Classes:
NetworkMonitor.NetworkMonitor$Impl

@Generated("org.javagi.JavaGI") public interface NetworkMonitor extends Proxy, Initable

GNetworkMonitor provides an easy-to-use cross-platform API for monitoring network connectivity. On Linux, the available implementations are based on the kernel's netlink interface and on NetworkManager.

There is also an implementation for use inside Flatpak sandboxes.

Since:
2.32
  • Method Details

    • getType

      static @Nullable Type getType()
      Get the GType of the NetworkMonitor class.
      Returns:
      the GType
    • getDefault

      static NetworkMonitor getDefault()
      Gets the default GNetworkMonitor for the system.
      Returns:
      a GNetworkMonitor, which will be a dummy object if no network monitor is available
      Since:
      2.32
    • canReach

      default boolean canReach(SocketConnectable connectable, @Nullable Cancellable cancellable) throws GErrorException

      Attempts to determine whether or not the host pointed to by connectable can be reached, without actually trying to connect to it.

      This may return true even when GNetworkMonitor:network-available is false, if, for example, this NetworkMonitor can determine that connectable refers to a host on a local network.

      If this NetworkMonitor believes that an attempt to connect to connectable will succeed, it will return true. Otherwise, it will return false and set error to an appropriate error (such as IOErrorEnum.HOST_UNREACHABLE).

      Note that although this does not attempt to connect to connectable, it may still block for a brief period of time (eg, trying to do multicast DNS on the local network), so if you do not want to block, you should use g_network_monitor_can_reach_async().

      Parameters:
      connectable - a GSocketConnectable
      cancellable - a GCancellable, or null
      Returns:
      true if connectable is reachable, false if not.
      Throws:
      GErrorException - see GError
      Since:
      2.32
    • canReachAsync

      default void canReachAsync(SocketConnectable connectable, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Asynchronously attempts to determine whether or not the host pointed to by connectable can be reached, without actually trying to connect to it.

      For more details, see g_network_monitor_can_reach().

      When the operation is finished, callback will be called. You can then call g_network_monitor_can_reach_finish() to get the result of the operation.

      Parameters:
      connectable - a GSocketConnectable
      cancellable - a GCancellable, or null
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • canReachFinish

      default boolean canReachFinish(AsyncResult result) throws GErrorException
      Finishes an async network connectivity test. See g_network_monitor_can_reach_async().
      Parameters:
      result - a GAsyncResult
      Returns:
      true if network is reachable, false if not.
      Throws:
      GErrorException - see GError
    • getConnectivity

      default NetworkConnectivity getConnectivity()

      Gets a more detailed networking state than g_network_monitor_get_network_available().

      If GNetworkMonitor:network-available is false, then the connectivity state will be NetworkConnectivity.LOCAL.

      If GNetworkMonitor:network-available is true, then the connectivity state will be NetworkConnectivity.FULL (if there is full Internet connectivity), NetworkConnectivity.LIMITED (if the host has a default route, but appears to be unable to actually reach the full Internet), or NetworkConnectivity.PORTAL (if the host is trapped behind a "captive portal" that requires some sort of login or acknowledgement before allowing full Internet access).

      Note that in the case of NetworkConnectivity.LIMITED and NetworkConnectivity.PORTAL, it is possible that some sites are reachable but others are not. In this case, applications can attempt to connect to remote servers, but should gracefully fall back to their "offline" behavior if the connection attempt fails.

      Returns:
      the network connectivity state
      Since:
      2.44
    • getNetworkAvailable

      default boolean getNetworkAvailable()
      Checks if the network is available. "Available" here means that the system has a default route available for at least one of IPv4 or IPv6. It does not necessarily imply that the public Internet is reachable. See GNetworkMonitor:network-available for more details.
      Returns:
      whether the network is available
      Since:
      2.32
    • getNetworkMetered

      default boolean getNetworkMetered()
      Checks if the network is metered. See GNetworkMonitor:network-metered for more details.
      Returns:
      whether the connection is metered
      Since:
      2.46
    • onNetworkChanged

      Emitted when the network configuration changes.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.32
      See Also:
    • emitNetworkChanged

      default void emitNetworkChanged(boolean networkAvailable)
      Emits the "network-changed" signal. See onNetworkChanged(NetworkMonitor.NetworkChangedCallback).