Created
October 12, 2020 09:52
-
-
Save stevyhacker/2beac2eda8e7eec2fc75c41eb5ccf137 to your computer and use it in GitHub Desktop.
Checking internet connectivity on 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
package com.infinum.onboarding.weatherapp.utils | |
import timber.log.Timber | |
import java.io.IOException | |
import java.net.InetSocketAddress | |
import java.net.Socket | |
import java.net.SocketAddress | |
class NetworkChecker { | |
companion object { | |
const val GOOGLE_DNS = "8.8.8.8" | |
const val DNS_PORT = 53 | |
const val TIMEOUT = 1500 | |
} | |
fun checkConnectivity(): Boolean { | |
return try { | |
val sock = Socket() | |
val socketAddress: SocketAddress = InetSocketAddress(GOOGLE_DNS, DNS_PORT) | |
sock.connect(socketAddress, TIMEOUT) | |
sock.close() | |
Timber.d("Network connection available") | |
true | |
} catch (e: IOException) { | |
Timber.d("Network connection not available") | |
false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment