Skip to content

Instantly share code, notes, and snippets.

@mvasilova
Created December 9, 2019 08:22
Show Gist options
  • Save mvasilova/de8cc8c1db3d9fd3eafb26bbe1355b20 to your computer and use it in GitHub Desktop.
Save mvasilova/de8cc8c1db3d9fd3eafb26bbe1355b20 to your computer and use it in GitHub Desktop.
Check InternetConnection - Android (Kotlin)
class NetworkHandler(private val context: Context) {
val isConnected get() = checkInternet()
private fun checkInternet(): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
var result = false
if (connectivityManager != null) {
result = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.activeNetwork != null
} else {
connectivityManager.activeNetworkInfo != null && connectivityManager.activeNetworkInfo?.isConnected ?: false
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment