Get Network type

Solution 1:

I hate magic numbers :

/**
 * You need to add:
 * 
 * <pre>
 *     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * </pre>
 * 
 * in your AndroidManifest.xml.
 */
private String networkType() {
    TelephonyManager teleMan = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = teleMan.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
        case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
        case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
        case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
        case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
        case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
        case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
        case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
        case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
        case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
        case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
        case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
        case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
        case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
        case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
        case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
    }
    throw new RuntimeException("New type of network");
}

Solution 2:

This works for me to check the network type...

TelephonyManager teleMan =  
            (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();

switch (networkType)
{
case 7:
    textV1.setText("1xRTT");
    break;      
case 4:
    textV1.setText("CDMA");
    break;      
case 2:
    textV1.setText("EDGE");
    break;  
case 14:
    textV1.setText("eHRPD");
    break;      
case 5:
    textV1.setText("EVDO rev. 0");
    break;  
case 6:
    textV1.setText("EVDO rev. A");
    break;  
case 12:
    textV1.setText("EVDO rev. B");
    break;  
case 1:
    textV1.setText("GPRS");
    break;      
case 8:
    textV1.setText("HSDPA");
    break;      
case 10:
    textV1.setText("HSPA");
    break;          
case 15:
    textV1.setText("HSPA+");
    break;          
case 9:
    textV1.setText("HSUPA");
    break;          
case 11:
    textV1.setText("iDen");
    break;
case 13:
    textV1.setText("LTE");
    break;
case 3:
    textV1.setText("UMTS");
    break;          
case 0:
    textV1.setText("Unknown");
    break;
}