Hello to all,
I've been, for several days in different sites, looking for a way to monitor when a network change occurs without success so far. This is show with a Toast message when the phone changes from 2G to 3G, 3G to LTE and vice versa.
I found that is needed to use TelephonyManager, Connectivity Manager and Broadcast Receiver.
So far I have the below code that shows the changes when phone is in WiFi, without connection or mobile data, but once is in status "Mobile data" is not reporting the changes between 2G, 3G, LTE anymore.
Is there a way to detect in the moment that occurs the mobile network type changes?
This is a Utility class code that I have so far working for android 6.
Thanks for any help.
I've been, for several days in different sites, looking for a way to monitor when a network change occurs without success so far. This is show with a Toast message when the phone changes from 2G to 3G, 3G to LTE and vice versa.
I found that is needed to use TelephonyManager, Connectivity Manager and Broadcast Receiver.
So far I have the below code that shows the changes when phone is in WiFi, without connection or mobile data, but once is in status "Mobile data" is not reporting the changes between 2G, 3G, LTE anymore.
Is there a way to detect in the moment that occurs the mobile network type changes?
This is a Utility class code that I have so far working for android 6.
Code:
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;
}
}
Thanks for any help.