import android.content.Context;
import android.telephony.TelephonyManager;
import android.widget.Toast;
/*
* 采用几G通道通信,通信时采用的网络类型.
* 几G的卡是移动运营商提供的移动通信通道类型,只有2G,3G。
* 获取sim卡是几G卡,步骤:
* 获得sim卡注册的移动运营商志识,获得移动运营商给sim卡提供的通信网络频段,然后判断是几G卡。
*
* 说明:
* 移动数据图标上面的E,G,H,3G网络图标不是这样判断的。通话网络和移动数据上网网络的网络类型不同
*/
* 获得移动数据通道类型
*/
public class NetworkTypeUtils {
public static final int NETWORK_TYPE_G = 0;
public static final int NETWORK_TYPE_E = 1;
public static final int NETWORK_TYPE_H = 2;
public static final int NETWORK_TYPE_3G = 3;
public static final int UNKNOWN_NETWORK_TYPE = -1;
static boolean mHspaDataDistinguishable = true;
public static int getNetworkType(Context context) {
try {
mHspaDataDistinguishable = context.getResources().getBoolean(
R.bool.config_hspa_data_distinguishable);
} catch (Exception e) {
mHspaDataDistinguishable = false;
}
TelephonyManager manager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = manager.getNetworkType();
if (networkType == TelephonyManager.NETWORK_TYPE_GPRS
|| networkType == TelephonyManager.NETWORK_TYPE_CDMA
|| networkType == TelephonyManager.NETWORK_TYPE_IDEN) {
//target = "2G";
return NETWORK_TYPE_G;
} else if (networkType == TelephonyManager.NETWORK_TYPE_EDGE
|| networkType == TelephonyManager.NETWORK_TYPE_1xRTT) {
//target = "2.75G";
return NETWORK_TYPE_E;
} else if (networkType == TelephonyManager.NETWORK_TYPE_UMTS
|| networkType == TelephonyManager.NETWORK_TYPE_EVDO_0
|| networkType == TelephonyManager.NETWORK_TYPE_EVDO_A
|| networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
//target = "3G";
return NETWORK_TYPE_3G;
} else if (networkType == TelephonyManager.NETWORK_TYPE_HSDPA
|| networkType == TelephonyManager.NETWORK_TYPE_HSUPA
|| networkType == TelephonyManager.NETWORK_TYPE_HSPA) {
if (mHspaDataDistinguishable) {
//target = "3.5G";
return NETWORK_TYPE_H;
} else {
//target = "3G";
return NETWORK_TYPE_3G;
}
} else {
//target = "未知网络";
return UNKNOWN_NETWORK_TYPE;
}
}
}
判断sim卡是几G卡的方法:
private boolean is3GSignal() {
if (serviceState != null) {
try {
String method = "getRadioTechnology";
Method mMethod = ServiceState.class.getMethod(method, null);
int networkType = (Integer) mMethod.invoke(serviceState, null);
// 由于这些静态属性都是hide注释的,所以可以通过反射来得到
Field f1 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_UMTS");
Field f2 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSDPA");
Field f3 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSUPA");
Field f4 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSPA");
Field f5 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_EVDO_0");
Field f6 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_EVDO_A");
f1.setAccessible(true);
f2.setAccessible(true);
f3.setAccessible(true);
f4.setAccessible(true);
f5.setAccessible(true);
f6.setAccessible(true);
if (networkType == f1.getInt(serviceState)
|| networkType == f2.getInt(serviceState)
|| networkType == f3.getInt(serviceState)
|| networkType == f4.getInt(serviceState)
|| networkType == f5.getInt(serviceState)
|| networkType == f6.getInt(serviceState)) {
return true;
}
Log.i("song", "3GSignal:" + networkType);
} catch(Exception e) {
e.printStackTrace();
}
}
return false;
}