Interface NetworkMonitor
- All Known Implementing Classes:
NetworkMonitor.NetworkMonitorImpl
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.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
NetworkMonitor.Builder<B extends Builder<B>>
static interface
Functional interface declaration of theNetworkChangedCallback
callback.static class
The NetworkMonitorImpl type represents a native instance of the NetworkMonitor interface.static class
The virtual function table forGNetworkMonitor
.Nested classes/interfaces inherited from interface org.gnome.gio.Initable
Initable.InitableIface, Initable.InitableImpl
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
canReach
(SocketConnectable connectable, @Nullable Cancellable cancellable) Attempts to determine whether or not the host pointed to byconnectable
can be reached, without actually trying to connect to it.default void
canReachAsync
(SocketConnectable connectable, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously attempts to determine whether or not the host pointed to byconnectable
can be reached, without actually trying to connect to it.default boolean
canReachFinish
(AsyncResult result) Finishes an async network connectivity test.default void
emitNetworkChanged
(boolean networkAvailable) Emits the "network-changed" signal.default NetworkConnectivity
Gets a more detailed networking state than g_network_monitor_get_network_available().static NetworkMonitor
Gets the defaultGNetworkMonitor
for the system.default boolean
Checks if the network is available.default boolean
Checks if the network is metered.static Type
getType()
Get the GType of the NetworkMonitor classEmitted when the network configuration changes.
-
Method Details
-
getType
-
getDefault
Gets the defaultGNetworkMonitor
for the system.- Returns:
- a
GNetworkMonitor
, which will be a dummy object if no network monitor is available
-
canReach
default boolean canReach(SocketConnectable connectable, @Nullable @Nullable Cancellable cancellable) throws GErrorException Attempts to determine whether or not the host pointed to byconnectable
can be reached, without actually trying to connect to it.This may return
true
even whenGNetworkMonitor
:network-available isfalse
, if, for example, this NetworkMonitor can determine thatconnectable
refers to a host on a local network.If this NetworkMonitor believes that an attempt to connect to
connectable
will succeed, it will returntrue
. Otherwise, it will returnfalse
and seterror
to 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
- aGSocketConnectable
cancellable
- aGCancellable
, ornull
- Returns:
true
ifconnectable
is reachable,false
if not.- Throws:
GErrorException
- seeGError
-
canReachAsync
default void canReachAsync(SocketConnectable connectable, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Asynchronously attempts to determine whether or not the host pointed to byconnectable
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
- aGSocketConnectable
cancellable
- aGCancellable
, ornull
callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
canReachFinish
Finishes an async network connectivity test. See g_network_monitor_can_reach_async().- Parameters:
result
- aGAsyncResult
- Returns:
true
if network is reachable,false
if 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.LIMITED
andNetworkConnectivity.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
-
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
-
getNetworkMetered
default boolean getNetworkMetered()Checks if the network is metered. SeeGNetworkMonitor
:network-metered for more details.- Returns:
- whether the connection is metered
-
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
- See Also:
-
emitNetworkChanged
default void emitNetworkChanged(boolean networkAvailable) Emits the "network-changed" signal. SeeonNetworkChanged(org.gnome.gio.NetworkMonitor.NetworkChangedCallback)
.
-