-
public final class NetworkConnectivityCheckerUtility for checking network connectivity state.
This class provides efficient connectivity checks by caching the network state and updating it via system callbacks, avoiding expensive system calls on each check.
val checker = NetworkConnectivityChecker(context) if (checker.isConnected()) { // Proceed with network operation }All methods are thread-safe. The connectivity state is tracked using atomic operations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classNetworkConnectivityChecker.Companion
-
Constructor Summary
Constructors Constructor Description NetworkConnectivityChecker(Context context)
-
Method Summary
Modifier and Type Method Description final BooleanisConnected()Check if the device currently has network connectivity. final BooleancheckConnectivityNow()Perform a synchronous connectivity check. -
-
Constructor Detail
-
NetworkConnectivityChecker
NetworkConnectivityChecker(Context context)
-
-
Method Detail
-
isConnected
final Boolean isConnected()
Check if the device currently has network connectivity.
This method checks for actual internet connectivity, not just network availability. A device might be connected to WiFi but have no internet access (captive portal, etc.).
-
checkConnectivityNow
final Boolean checkConnectivityNow()
Perform a synchronous connectivity check.
This method queries the system directly rather than using cached state. Use isConnected for most cases as it's more efficient.
-
-
-
-