Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@agent10
Created March 6, 2020 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agent10/cd34308df7c1b42f83bbb3750e2a8ce9 to your computer and use it in GitHub Desktop.
Save agent10/cd34308df7c1b42f83bbb3750e2a8ce9 to your computer and use it in GitHub Desktop.
Simple check internet connection
class InternetChecker @Inject constructor() {
fun hasInternet(): Single<Boolean> {
return Single.fromCallable {
try {
// Connect to Google DNS to check for connection
val timeoutMs = 1500
val socket = Socket()
val socketAddress = InetSocketAddress("8.8.8.8", 53)
socket.connect(socketAddress, timeoutMs)
socket.close()
true
} catch (e: Exception) {
Log.e("InternetChecker", "No internet connection: $e")
false
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment