Created
March 6, 2020 16:03
-
-
Save agent10/cd34308df7c1b42f83bbb3750e2a8ce9 to your computer and use it in GitHub Desktop.
Simple check internet connection
This file contains hidden or 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
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