I wanted to constantly check for internet connection status in order to block my Application (with a dialog window informing the issue) until the network is re-established. Since I'm not using Compose for Android, I don't have access to ConnectivityManager class as I've seen been used in other related answers to this question online. How can I achieve this in Compose for Desktop?
Asked
Active
Viewed 272 times
1
-
2Compose for Desktop is UI framework, it has nothing to do with internet connection. In such cases you need to search for platform-related answers - in this case your platform is JVM, so something like "jvm interner reachability" would give you [How to check if internet connection is present in Java?](https://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java) – Phil Dukhov Feb 20 '23 at 10:32
1 Answers
1
Basically Compose for Desktop means that you can use any JVM/Java/Kotlin way of checking for Internet connectivity.
One option would be to use the NetworkUtils
class provided by the java.net
package.
import java.net.InetAddress
import java.net.UnknownHostException
fun isInternetAvailable(): Boolean {
return try {
val address = InetAddress.getByName("www.google.com")
address != null && !address.equals("")
} catch (e: UnknownHostException) {
false
}
}
You are checking if you can reach Google by trying to resolve it's IP address.
Be aware that this approach is not foolproof and there may be cases where the internet is available but the Google website is not reachable (e.g. due to firewall restrictions).

Yolgie
- 268
- 3
- 16