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;
}
}