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
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
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";
}
throw new RuntimeException("New type of network");
}
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.