How to show a slow internet connection to the user when the network is connected Note: Not a network type (2G,3G,4G, WIFI)
to determine Network speed in android programmatically Use this util class may helpful to your query
need to permission in the manifest:
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.telephony.TelephonyManager
public object ConnectivityUtil {
/**
* Get the network info
* @param context
* @return
*/
fun getNetworkInfo(context: Context): NetworkInfo? {
val cm =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return cm.activeNetworkInfo
}
/**
* Check if there is any connectivity
* @param context
* @return
*/
fun isConnected(context: Context): Boolean {
val info = getNetworkInfo(context)
return info != null && info.isConnected
}
/**
* Check if there is any connectivity to a Wifi network
* @param context
*
* @return
*/
fun isConnectedWifi(context: Context): Boolean {
val info = getNetworkInfo(context)
return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_WIFI
}
/**
* Check if there is any connectivity to a mobile network
* @param context
*
* @return
*/
fun isConnectedMobile(context: Context): Boolean {
val info = getNetworkInfo(context)
return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_MOBILE
}
/**
* Check if there is fast connectivity
* @param context
* @return
*/
fun isConnectedFast(context: Context): Boolean {
val info = getNetworkInfo(context)
return info != null && info.isConnected && isConnectionFast(
info.type,
info.subtype
)
}
/**
* Check if the connection is fast
*
* @param subType
* @return
*/
fun isConnectionFast(type: Int, subType: Int): Boolean {
return if (type == ConnectivityManager.TYPE_WIFI) {
true
} else if (type == ConnectivityManager.TYPE_MOBILE) {
when (subType) {
TelephonyManager.NETWORK_TYPE_1xRTT -> false // ~ 50-100 kbps
TelephonyManager.NETWORK_TYPE_CDMA -> false // ~ 14-64 kbps
TelephonyManager.NETWORK_TYPE_EDGE -> false // ~ 50-100 kbps
TelephonyManager.NETWORK_TYPE_EVDO_0 -> true // ~ 400-1000 kbps
TelephonyManager.NETWORK_TYPE_EVDO_A -> true // ~ 600-1400 kbps
TelephonyManager.NETWORK_TYPE_GPRS -> false // ~ 100 kbps
TelephonyManager.NETWORK_TYPE_HSDPA -> true // ~ 2-14 Mbps
TelephonyManager.NETWORK_TYPE_HSPA -> true // ~ 700-1700 kbps
TelephonyManager.NETWORK_TYPE_HSUPA -> true // ~ 1-23 Mbps
TelephonyManager.NETWORK_TYPE_UMTS -> true // ~ 400-7000 kbps
TelephonyManager.NETWORK_TYPE_EHRPD -> true // ~ 1-2 Mbps
TelephonyManager.NETWORK_TYPE_EVDO_B -> true // ~ 5 Mbps
TelephonyManager.NETWORK_TYPE_HSPAP -> true // ~ 10-20 Mbps
TelephonyManager.NETWORK_TYPE_IDEN -> false // ~25 kbps
TelephonyManager.NETWORK_TYPE_LTE -> true // ~ 10+ Mbps
TelephonyManager.NETWORK_TYPE_UNKNOWN -> false
else -> false
}
} else {
false
}
}
}
Output:
Log.d("feby","Network info : "+ getNetworkInfo(getApplicationContext()));
//[type: MOBILE[LTE] - MOBILE, state: CONNECTED/CONNECTED, reason: connected, extra: jionet, roaming: false, failover: false, isAvailable: true]
Log.d("feby","Is connected : "+ isConnected(getApplicationContext())); //true
Log.d("feby","Is connected wifi : "+ isConnectedWifi(getApplicationContext())); //false
Log.d("feby","Is connected mobile : "+ isConnectedMobile(getApplicationContext())); //true
Log.d("feby","Is connected Fast : "+ isConnectedFast(getApplicationContext())); // true