How to determine if network type is 2G, 3G or 4G
Asked Answered
O

6

75

I have an indicator on my application to display the network type (2G or 3G or 4G) but after getting the network type, how do I know what speed category it should be in?

I know how to detect the network type:

private TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CurrentNetworkType = telephonyManager.getNetworkType();

Given the possible return values:

//   public static final int NETWORK_TYPE_1xRTT
//   Since: API Level 4
//   Current network is 1xRTT
//   Constant Value: 7 (0x00000007)
//   
//   public static final int NETWORK_TYPE_CDMA
//   Since: API Level 4
//   Current network is CDMA: Either IS95A or IS95B
//   Constant Value: 4 (0x00000004)
//   
//   public static final int NETWORK_TYPE_EDGE
//   Since: API Level 1
//   Current network is EDGE
//   Constant Value: 2 (0x00000002)
//   
//   public static final int NETWORK_TYPE_EHRPD
//   Since: API Level 11
//   Current network is eHRPD
//   Constant Value: 14 (0x0000000e)
//   
//   public static final int NETWORK_TYPE_EVDO_0
//   Since: API Level 4
//   Current network is EVDO revision 0
//   Constant Value: 5 (0x00000005)
//   
//   public static final int NETWORK_TYPE_EVDO_A
//   Since: API Level 4
//   Current network is EVDO revision A
//   Constant Value: 6 (0x00000006)
//   
//   public static final int NETWORK_TYPE_EVDO_B
//   Since: API Level 9
//   Current network is EVDO revision B
//   Constant Value: 12 (0x0000000c)
//   
//   public static final int NETWORK_TYPE_GPRS
//   Since: API Level 1
//   Current network is GPRS
//   Constant Value: 1 (0x00000001)
//   
//   public static final int NETWORK_TYPE_HSDPA
//   Since: API Level 5
//   Current network is HSDPA
//   Constant Value: 8 (0x00000008)
//   
//   public static final int NETWORK_TYPE_HSPA
//   Since: API Level 5
//   Current network is HSPA
//   Constant Value: 10 (0x0000000a)
//   
//   public static final int NETWORK_TYPE_HSPAP
//   Since: API Level 13
//   Current network is HSPA+
//   Constant Value: 15 (0x0000000f)
//   
//   public static final int NETWORK_TYPE_HSUPA
//   Since: API Level 5
//   Current network is HSUPA
//   Constant Value: 9 (0x00000009)
//   
//   public static final int NETWORK_TYPE_IDEN
//   Since: API Level 8
//   Current network is iDen
//   Constant Value: 11 (0x0000000b)
//   
//   public static final int NETWORK_TYPE_LTE
//   Since: API Level 11
//   Current network is LTE
//   Constant Value: 13 (0x0000000d)
//   
//   public static final int NETWORK_TYPE_UMTS
//   Since: API Level 1
//   Current network is UMTS
//   Constant Value: 3 (0x00000003)
//   
//   public static final int NETWORK_TYPE_UNKNOWN
//   Since: API Level 1
//   Network type is unknown
//   Constant Value: 0 (0x00000000)

I would consider LTE to be 4G, but which of these are really considered 3G? Anything else I would consider 2G.

So where do you draw the line between 3G or not 3G?

Update: I found another relevant answer at https://mcmap.net/q/127723/-detect-network-connection-type-on-android It uses ConnectivityManager() to get type and subtype and then classifies the subtype as either fast or not. I don't know if using ConnectivityManager() is a better approach then using TelephonyManager () since they both appear able to return the network type.

Also I found a link that compares wireless data standards at http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards.

Outrider answered 14/2, 2012 at 20:32 Comment(1)
possible duplicate of Detect network connection type on AndroidPitching
H
114

You can put this following method directly in your Utility class:

Kotlin:

/** Usage: `networkTypeClass(telephonyManager.networkType)` */
fun networkTypeClass(networkType: Int): String {
    when (networkType) {
        TelephonyManager.NETWORK_TYPE_GPRS,
        TelephonyManager.NETWORK_TYPE_EDGE,
        TelephonyManager.NETWORK_TYPE_CDMA,
        TelephonyManager.NETWORK_TYPE_1xRTT,
        TelephonyManager.NETWORK_TYPE_IDEN,
        TelephonyManager.NETWORK_TYPE_GSM
        -> return "2G"
        TelephonyManager.NETWORK_TYPE_UMTS,
        TelephonyManager.NETWORK_TYPE_EVDO_0,
        TelephonyManager.NETWORK_TYPE_EVDO_A,
        TelephonyManager.NETWORK_TYPE_HSDPA,
        TelephonyManager.NETWORK_TYPE_HSUPA,
        TelephonyManager.NETWORK_TYPE_HSPA,
        TelephonyManager.NETWORK_TYPE_EVDO_B,
        TelephonyManager.NETWORK_TYPE_EHRPD,
        TelephonyManager.NETWORK_TYPE_HSPAP,
        TelephonyManager.NETWORK_TYPE_TD_SCDMA
        -> return "3G"
        TelephonyManager.NETWORK_TYPE_LTE
        -> return "4G"
        TelephonyManager.NETWORK_TYPE_NR
        -> return "5G"
        else -> return "Unknown"
    }
}

Java:

public String getNetworkClass(Context context) {
    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return "2G";
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return "3G";
        case TelephonyManager.NETWORK_TYPE_LTE:
            return "4G";
        case TelephonyManager.NETWORK_TYPE_NR:
            return "5G";
        default:
            return "Unknown";
    }
}

Thanks to assistance from the Android source code. =]

Haden answered 3/9, 2013 at 2:13 Comment(1)
can u help me, how to use this code in dual sim enable mobile ?Filling
S
81

Based on the Android Developer document & Wikipedia link here i have given comments & define network type.Check the links in comment.

You can use getNetworkType to get Network type.

        public class CommonUtils {

    /**
     * To get device consuming netowkr type is 2g,3g,4g
     *
     * @param context
     * @return "2g","3g","4g" as a String based on the network type
     */
    public static String getNetworkType(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int networkType = mTelephonyManager.getNetworkType();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2g";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                /**
                 From this link https://en.wikipedia.org/wiki/Evolution-Data_Optimized ..NETWORK_TYPE_EVDO_0 & NETWORK_TYPE_EVDO_A
                 EV-DO is an evolution of the CDMA2000 (IS-2000) standard that supports high data rates.

                 Where CDMA2000 https://en.wikipedia.org/wiki/CDMA2000 .CDMA2000 is a family of 3G[1] mobile technology standards for sending voice,
                 data, and signaling data between mobile phones and cell sites.
                 */
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                //Log.d("Type", "3g");
                //For 3g HSDPA , HSPAP(HSPA+) are main  networktype which are under 3g Network
                //But from other constants also it will 3g like HSPA,HSDPA etc which are in 3g case.
                //Some cases are added after  testing(real) in device with 3g enable data
                //and speed also matters to decide 3g network type
                //https://en.wikipedia.org/wiki/4G#Data_rate_comparison
                return "3g";
            case TelephonyManager.NETWORK_TYPE_LTE:
                //No specification for the 4g but from wiki
                //I found(LTE (Long-Term Evolution, commonly marketed as 4G LTE))
                //https://en.wikipedia.org/wiki/LTE_(telecommunication)
                return "4g";
            default:
                return "Notfound";
        }
    }

    /**
     * To check device has internet
     *
     * @param context
     * @return boolean as per status
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnected();
    }
}
Synectics answered 27/6, 2013 at 11:21 Comment(7)
if i connected to wifi if i run this i am getting gprs can you resolveHalvaard
@user1140237:For my application I require constant internet connection,for which I am using a service to detect if the internet is turned on or not,if not then turn on the internet.Sometimes I face a situation wherein the data connection would have been turned on long back, but due to some issue, the internet symbol(H,E etc) ie shown over the signal strength symbol(in the notification) becomes absent.In this context where the internet is shown as on BUT the internet symbol is absent ,if I use your above snippet then can it detect if the symbol is present or not ?Afterbirth
@Afterbirth to check device have internet connection or not you can use CONNECTIVITY_ACTION developer.android.com/reference/android/net/… ...and there you can check if the internet is connected thn is it 2g/3g ? hope i hve understand ur issue... if wants more ask separate question for tht issue..Synectics
My phone has 3G, but this method give me result of NETWORK_TYPE_HSPAP , which is a wrong detection. @Anonsage's answer may be more correct.Bebeeru
Is this expensive to call before each network request. I would like to make reftofit interceptor and send to web service info about network (in headers)?Beriberi
@Vlado Nope its not expensive to call. but it is required to call before any api( or any network related call) because if network is not consistent in that case api request(call) get failed. still if you really wants optimize you can get instance of TelephonyManager instance in you Activity or fragment createSynectics
@Synectics can u help me, how to use this code in dual sim enable mobile ?Elvia
H
10

You can use getSubtype() for more details.

int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
    return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
    && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
    && !mTelephony.isNetworkRoaming()) {
        return info.isConnected();
} else {
    return false;
}
Hunch answered 14/2, 2012 at 20:40 Comment(1)
Would be great to know which type "info" has ;) ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo();Telemachus
E
5

Updated version for API 29 / Android Q with 5G support in Kotlin:

enum class Generation {
    `2G`,
    `3G`,
    `4G`,
    `5G`
}

fun getNetworkGeneration(context: Context): Generation? {
    val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    return when (telephonyManager.networkType) {
        TelephonyManager.NETWORK_TYPE_UNKNOWN -> null

        TelephonyManager.NETWORK_TYPE_GPRS,
        TelephonyManager.NETWORK_TYPE_EDGE,
        TelephonyManager.NETWORK_TYPE_CDMA,
        TelephonyManager.NETWORK_TYPE_1xRTT,
        TelephonyManager.NETWORK_TYPE_IDEN,
        TelephonyManager.NETWORK_TYPE_GSM -> Generation.`2G`

        TelephonyManager.NETWORK_TYPE_UMTS,
        TelephonyManager.NETWORK_TYPE_EVDO_0,
        TelephonyManager.NETWORK_TYPE_EVDO_A,
        TelephonyManager.NETWORK_TYPE_HSDPA,
        TelephonyManager.NETWORK_TYPE_HSUPA,
        TelephonyManager.NETWORK_TYPE_HSPA,
        TelephonyManager.NETWORK_TYPE_EVDO_B,
        TelephonyManager.NETWORK_TYPE_EHRPD,
        TelephonyManager.NETWORK_TYPE_HSPAP,
        TelephonyManager.NETWORK_TYPE_TD_SCDMA -> Generation.`3G`

        TelephonyManager.NETWORK_TYPE_LTE,
        TelephonyManager.NETWORK_TYPE_IWLAN -> Generation.`4G`

        TelephonyManager.NETWORK_TYPE_NR -> Generation.`5G`

        else -> null
    }
}

Use it like so in your activity:

val generation = getNetworkGeneration(this)
when (generation) {
    Generation.`2G` -> TODO()
    Generation.`3G` -> TODO()
    Generation.`4G` -> TODO()
    Generation.`5G` -> TODO()
    null -> TODO()
}
Emilemile answered 23/7, 2019 at 15:11 Comment(0)
T
3

Technically speaking 1xRTT is a 3G technology (although many consider it 2G based solely on data speed). Also you will want to add WiMax to your switch statement to return 4G. It's not used often anymore, but Sprint's WiMax network is still operational for the time being.

Turbulent answered 7/12, 2013 at 4:59 Comment(1)
Reg. 1xRTT, from where do have that info? Please provide a link. Also do you mean IWLAN for WiMax?Calash
G
1

I think really you just have to hardcode the equivalent value you want them to have. A quick googling of most of those technologies should give you some manner of an idea as to which is considered 3G or 4G (though technically none of them are real 4G). Since there doesn't seem to be a distinction between HSPA and HSPA+, you might want to run some sort of speed or latency check, and go with it that way.

Gareri answered 14/2, 2012 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.