Created
November 12, 2019 15:58
-
-
Save adityabhaskar/0d3e222121d289502079b81e53ab303f to your computer and use it in GitHub Desktop.
Android Check Internet Connection
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
import android.net.ConnectivityManager | |
import android.net.NetworkCapabilities | |
import android.content.Context | |
object Utils { | |
internal fun isOnline(context: Context): Boolean { | |
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetwork = cm.activeNetwork ?: return false | |
val capabilities = cm.getNetworkCapabilities(activeNetwork) ?: return false | |
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment