Skip to content

Instantly share code, notes, and snippets.

@adityabhaskar
Created November 12, 2019 15:58
Show Gist options
  • Save adityabhaskar/0d3e222121d289502079b81e53ab303f to your computer and use it in GitHub Desktop.
Save adityabhaskar/0d3e222121d289502079b81e53ab303f to your computer and use it in GitHub Desktop.
Android Check Internet Connection
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