Created
December 9, 2019 08:22
-
-
Save mvasilova/de8cc8c1db3d9fd3eafb26bbe1355b20 to your computer and use it in GitHub Desktop.
Check InternetConnection - Android (Kotlin)
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
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