How do you detect the network connection type on Android?
Is it through ConnectivityManager.getActiveNetworkInfo().getType()
, and is the answer limited
I use this simple code:
fun getConnectionInfo(): ConnectionInfo {
val cm = appContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return if (cm.activeNetwork == null) {
ConnectionInfo.NO_CONNECTION
} else {
if (cm.isActiveNetworkMetered) {
ConnectionInfo.MOBILE
} else {
ConnectionInfo.WI_FI
}
}
}