GPRS 区分2g/3g/4g网络

时间:2022-02-24 17:23:56
/**
* 区分2g/3g/4gGPRG网络
* @author Bubble
*
*/



import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.SyncStateContract.Constants;
import android.telephony.TelephonyManager;

public class GPRSNetType {
private static final int NETWORK_TYPE_UNAVAILABLE = -1;
private static final int NETWORK_TYPE_WIFI = -101;

private static final int NETWORK_CLASS_WIFI = -101;
private static final int NETWORK_CLASS_UNAVAILABLE = -1;
/** Unknown network class. */
private static final int NETWORK_CLASS_UNKNOWN = 0;
private static final int NETWORK_CLASS_2_G = 1;
private static final int NETWORK_CLASS_3_G = 2;
private static final int NETWORK_CLASS_4_G = 3;

// 适配低版本手机
public static final int NETWORK_TYPE_UNKNOWN = 0;
public static final int NETWORK_TYPE_GPRS = 1;
public static final int NETWORK_TYPE_EDGE = 2;
public static final int NETWORK_TYPE_UMTS = 3;
public static final int NETWORK_TYPE_CDMA = 4;
public static final int NETWORK_TYPE_EVDO_0 = 5;
public static final int NETWORK_TYPE_EVDO_A = 6;
public static final int NETWORK_TYPE_1xRTT = 7;
public static final int NETWORK_TYPE_HSDPA = 8;
public static final int NETWORK_TYPE_HSUPA = 9;
public static final int NETWORK_TYPE_HSPA = 10;
public static final int NETWORK_TYPE_IDEN = 11;
public static final int NETWORK_TYPE_EVDO_B = 12;
public static final int NETWORK_TYPE_LTE = 13;
public static final int NETWORK_TYPE_EHRPD = 14;
public static final int NETWORK_TYPE_HSPAP = 15;

/**
*
* @param context
* @return
*/

static int getNetworkClass(Context context) {
int networkType = NETWORK_TYPE_UNKNOWN;
try {
final NetworkInfo network = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (network != null && network.isAvailable()
&& network.isConnected()) {
int type = network.getType();
if (type == ConnectivityManager.TYPE_WIFI) {
networkType = NETWORK_TYPE_WIFI;
} else if (type == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
networkType = telephonyManager.getNetworkType();
}
} else {
networkType = NETWORK_TYPE_UNAVAILABLE;
}

} catch (Exception ex) {
ex.printStackTrace();
}
return getNetworkClassByType(networkType);

}

/**
* 根据网络类型区分2g/3g/4g
* @param networkType
* @return
*/

private static int getNetworkClassByType(int networkType) {
switch (networkType) {
case NETWORK_TYPE_UNAVAILABLE:
return NETWORK_CLASS_UNAVAILABLE;
case NETWORK_TYPE_WIFI:
return NETWORK_CLASS_WIFI;
case NETWORK_TYPE_GPRS:
case NETWORK_TYPE_EDGE:
case NETWORK_TYPE_CDMA:
case NETWORK_TYPE_1xRTT:
case NETWORK_TYPE_IDEN:
return NETWORK_CLASS_2_G
case NETWORK_TYPE_UMTS:
case NETWORK_TYPE_EVDO_0:
case NETWORK_TYPE_EVDO_A:
case NETWORK_TYPE_HSDPA:
case NETWORK_TYPE_HSUPA:
case NETWORK_TYPE_HSPA:
case NETWORK_TYPE_EVDO_B:
case NETWORK_TYPE_EHRPD:
case NETWORK_TYPE_HSPAP:
return NETWORK_CLASS_3G
case NETWORK_TYPE_LTE:
return NETWORK_CLASS_4G;
default:
return NETWORK_CLASS_UNKNOWN;
}
}


/**
* 获取当前的网络类型
*
* @param context
* @return
*/

public static String getCurrentNetworkType(Context context) {
int networkClass = getNetworkClass(context);
String type = "未知";
switch (networkClass) {
case NETWORK_CLASS_UNAVAILABLE:
type = "无";
break;
case NETWORK_CLASS_WIFI:
type = "Wi-Fi";
break;
case Constant.NETWORK_CLASS_2G:
type = "2G";
break;
case Constant.NETWORK_CLASS_3G:
type = "3G";
break;
case Constant.NETWORK_CLASS_4G:
type = "4G";
break;
case Constant.NETWORK_CLASS_RESERVE0:
type = "未知";
break;
}
return type;
}

}