In this tip, I would like to present a solution to check cellular network info (type and speed of the connection) which available on your device through using
TelephonyManager
class.
Check if device has a connection
getActiveNetworkInfo()
method of NetworkInfo
class with an initialized ConnectivityManager
instance, you can do this work easily:
public static String isConected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if ((info != null && info.isConnected())) {
return checkingNetworkSpeed(info.getType(), info.getSubtype());
} else
return "No NetWork Access";
}
Checking the network type
NetworkInfo
class, we can get the network type by 2 methods:
getType()
: returnConnectivityManager.TYPE_WIFI
if your device is connection via Wifi and returnConnectivityManager.TYPE_MOBILE
if your device is using cellular network.getSubtype()
: return a network-type-specific integer describing the sub-type of the network.
public static String checkingNetworkSpeed(int type, int subType) {
if (type == ConnectivityManager.TYPE_WIFI) {
return "CONNECTED VIA WIFI";
} else if (type == ConnectivityManager.TYPE_MOBILE) {
switch (subType) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "NETWORK TYPE 1xRTT - Speed: ~50 - 100 Kbps";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "NETWORK TYPE CDMA (3G) Speed: ~14-64 Kbps";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "NETWORK TYPE EDGE (2.75G) Speed: 100-120 Kbps";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "NETWORK TYPE EVDO_0 Speed: ~400-1000 Kbps";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "NETWORK TYPE EVDO_A Speed: ~600-1400 Kbps";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "NETWORK TYPE GPRS (2.5G) Speed: ~100 Kbps";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "NETWORK TYPE HSDPA (4G) Speed: 2-14 Mbps";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "NETWORK TYPE HSPA (4G) Speed: 0.7-1.7 Mbps";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "NETWORK TYPE HSUPA (3G) Speed: 1-23 Mbps";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "NETWORK TYPE UMTS (3G) Speed: 0.4-7 Mbps";
// API level 7 not supported this type
case NETWORK_TYPE_EHRPD:
return "NETWORK TYPE EHRPD Speed: ~1-2 Mbps";
case NETWORK_TYPE_EVDO_B:
return "NETWORK_TYPE_EVDO_B Speed: ~5 Mbps";
case NETWORK_TYPE_HSPAP:
return "NETWORK TYPE HSPA+ (4G) Speed: 10-20 Mbps";
case NETWORK_TYPE_IDEN:
return "NETWORK TYPE IDEN Speed: ~25 Kbps";
case NETWORK_TYPE_LTE:
return "NETWORK TYPE LTE (4G) Speed: 10+ Mbps";
// Unknown type
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "NETWORK TYPE UNKNOWN";
default:
return "";
}
} else {
return "";
}
}
Of course, these are constants aren't available in API level 7 that we must define:
public static final int NETWORK_TYPE_EHRPD = 14; // API Level 11
public static final int NETWORK_TYPE_EVDO_B = 12; // API Level 9
public static final int NETWORK_TYPE_HSPAP = 15; // API Level 13
public static final int NETWORK_TYPE_IDEN = 11; // API Level 8
public static final int NETWORK_TYPE_LTE = 13; // API Level 11
Adding check network state permission
ACCESS_NETWORK_STATE
permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Running an activity to test
MainActivity
class, modifying onCreate()
like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView networkInfo = (TextView) findViewById(R.id.network_type);
String type = isConected(this);
networkInfo.setText(type);
}
Running this application when your device is connecting via Wifi, you will have this output:
Now, disabled wifi connection and enable the cellular network on your device, you'll see it's type and speed: