Interface NetworkMonitor
- All Known Implementing Classes:
NetworkMonitor.NetworkMonitor$Impl
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
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceNetworkMonitor.Builder<B extends Builder<B>>static interfaceFunctional interface declaration of theNetworkChangedCallbackcallback.static classThe NetworkMonitor$Impl type represents a native instance of the NetworkMonitor interface.static classThe virtual function table forGNetworkMonitor.Nested classes/interfaces inherited from interface org.gnome.gio.Initable
Initable.Initable$Impl, Initable.InitableIface -
Method Summary
Modifier and TypeMethodDescriptiondefault booleancanReach(SocketConnectable connectable, @Nullable Cancellable cancellable) Attempts to determine whether or not the host pointed to byconnectablecan be reached, without actually trying to connect to it.default voidcanReachAsync(SocketConnectable connectable, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously attempts to determine whether or not the host pointed to byconnectablecan be reached, without actually trying to connect to it.default booleancanReachFinish(AsyncResult result) Finishes an async network connectivity test.default voidemitNetworkChanged(boolean networkAvailable) Emits the "network-changed" signal.default NetworkConnectivityGets a more detailed networking state than g_network_monitor_get_network_available().static NetworkMonitorGets the defaultGNetworkMonitorfor the system.default booleanChecks if the network is available.default booleanChecks if the network is metered.static @Nullable TypegetType()Get the GType of the NetworkMonitor classEmitted when the network configuration changes.
-
Method Details
-
getType
-
getDefault
Gets the defaultGNetworkMonitorfor 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 byconnectablecan be reached, without actually trying to connect to it.This may return
trueeven whenGNetworkMonitor:network-available isfalse, if, for example, this NetworkMonitor can determine thatconnectablerefers to a host on a local network.If this NetworkMonitor believes that an attempt to connect to
connectablewill succeed, it will returntrue. Otherwise, it will returnfalseand seterrorto an appropriate error (such asIOErrorEnum.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- aGSocketConnectablecancellable- aGCancellable, ornull- Returns:
trueifconnectableis reachable,falseif not.- Throws:
GErrorException- seeGError- 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 byconnectablecan be reached, without actually trying to connect to it.For more details, see g_network_monitor_can_reach().
When the operation is finished,
callbackwill be called. You can then call g_network_monitor_can_reach_finish() to get the result of the operation.- Parameters:
connectable- aGSocketConnectablecancellable- aGCancellable, ornullcallback- aGAsyncReadyCallbackto call when the request is satisfied
-
canReachFinish
Finishes an async network connectivity test. See g_network_monitor_can_reach_async().- Parameters:
result- aGAsyncResult- Returns:
trueif network is reachable,falseif not.- Throws:
GErrorException- seeGError
-
getConnectivity
Gets a more detailed networking state than g_network_monitor_get_network_available().If
GNetworkMonitor:network-available isfalse, then the connectivity state will beNetworkConnectivity.LOCAL.If
GNetworkMonitor:network-available istrue, then the connectivity state will beNetworkConnectivity.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), orNetworkConnectivity.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.LIMITEDandNetworkConnectivity.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. SeeGNetworkMonitor:network-available for more details.- Returns:
- whether the network is available
- Since:
- 2.32
-
getNetworkMetered
default boolean getNetworkMetered()Checks if the network is metered. SeeGNetworkMonitor:network-metered for more details.- Returns:
- whether the connection is metered
- Since:
- 2.46
-
onNetworkChanged
default SignalConnection<NetworkMonitor.NetworkChangedCallback> onNetworkChanged(NetworkMonitor.NetworkChangedCallback handler) 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. SeeonNetworkChanged(org.gnome.gio.NetworkMonitor.NetworkChangedCallback).
-