1

I've been trying and looking in several sites and here in SO without success to find a solution for my goal.

I was able to detect the events when network changes from WiFi, Mobile, No connection following this example using a NetworkUtil class and a Broadcaster Receiver.

My issue is that I can't find a way to detect (within de MOBILE status) when network type changes from LTE to 3G, 3G to LTE, 2G to 3G, etc.

I came up with the NetworkUtil class below but even I'm using TelephonyManager doesn't report when there is a change in NETWORK_TYPE

Someone could help me out with this issue please. Thanks in advance.

class NetworkUtil{

public static String getConnectivityStatusString(Context context) {
    String status = null;
    String mobile_status = null;

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();


    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
            mobile_status = "2G"; break;
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_HSPA:
            mobile_status = "3G"; break;
        default:
            mobile_status = "Unknown"; break;
    }


    if (activeNetwork != null) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            status = "Wifi enabled";
            return status;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "2G") {
            status = "2G enabled";
            return status;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "3G") {
            status = "3G enabled";
            return status;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "Unknown") {
            status = "Mobile unknown enabled";
            return status;
        }
    } else {
        status = "No internet is available";
        return status;
    }

    return status;
}

}

Ger Cas
  • 2,188
  • 2
  • 18
  • 45
  • https://stackoverflow.com/questions/51304143/how-detect-networktype-2g-3g-lte-when-connected-to-wifi?rq=1 – kiran boghra Aug 22 '19 at 06:41
  • Thank you for the link shared. I've checked it but even when the code in that post detects the differente network types it doesn't seem to trigger the event when changes from 3G to 4G and vice – Ger Cas Aug 22 '19 at 18:52

0 Answers0