Android TIP: Get Cellular Network information

    Almost all Android devices support cellular network. At some point, we want to know whether the device is connected to network so that we can do some network processes. Also we want to know if user make WiFi or 3G/4G disabled on purpose. The fact that many applications have warned user when their devices are using cellular network because of it's fee and speed.
    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

    Firstly, you should check if your device has a connectivity or not. By using 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

    Also by NetworkInfo class, we can get the network type by 2 methods:
  • getType(): return ConnectivityManager.TYPE_WIFI if your device is connection via Wifi and return ConnectivityManager.TYPE_MOBILE if your device is using cellular network.
  • getSubtype(): return a network-type-specific integer describing the sub-type of the network.
    For all mobile network types and their speed, please look at this code:
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

    Every time you want to check the network information, you must add ACCESS_NETWORK_STATE permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Running an activity to test

    After put all codes above to your 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:

Conclusions

    Now, you've known the solution to get cellular network information in Android serve your own purposes. Further, please read these official document to take a deep understanding about networking in Android:

Share


Previous post
« Prev Post
Next post
Next Post »