I\'ve been trying to retrive the current network type, but no success
when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
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;
}
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
In the Android SDK in TelephonyManager there is a hidden method that has to do with the mobile data classes, it can be useful to follow the changes of this method through the API levels to keep up to date in the definition of android for them
Note: The source code shown is from API 29 of the Android SDK
In Android API LEVEL 30 getNetworkType has deprecated. Use getDataNetworkType() , and provide android.permission.READ_PHONE_STATE
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.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";
}
return "New type of network";
}