Created
July 31, 2022 00:23
-
-
Save nisrulz/ffe76f40284bb35828640c1ed7db79cd to your computer and use it in GitHub Desktop.
Check if Internet is available in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun isInternetAvailable(): Boolean { | |
return try { | |
val timeoutMs = 1500 | |
val sock = Socket() | |
val sockaddr = InetSocketAddress("8.8.8.8", 53) | |
sock.connect(sockaddr, timeoutMs) | |
sock.close() | |
true | |
} catch (e: IOException) { | |
false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment