android 判断 网络 类型

1.获取sim卡类型

private static int getMobileType(Context context) 

{
int type = -1; //无卡
TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String iNumeric = iPhoneManager.getSimOperator();
if (iNumeric.length() > 0)
{
if (iNumeric.equals("46000") || iNumeric.equals("46002"))
{
// 中国移动
type=0;
}
else if (iNumeric.equals("46001"))
{
// 中国联通
type=1;
}
else if (iNumeric.equals("46003"))
{
// 中国电信
type=2;
}
}
return type;

}

2./** 获取网络信息 */

// 网络状态
public static final String UNKNWON = "unkwon";
public static final String NOT_AVAILABLE = "not_avaible";
public static final String WIFI = "wifi";
public static final String G3NET = "3gnet";
public static final String G3WAP = "3gwap";
public static final String UNINET = "uninet";
public static final String UNIWAP = "uniwap";
public static final String CMNET = "cmnet";
public static final String CMWAP = "cmwap";
public static final String CTNET = "ctnet";
public static final String CTWAP = "ctwap";
public static final String MOBILE = "mobile";

public static String getNetApn() {
if (CenterApp.getContext().checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") == PackageManager.PERMISSION_DENIED) {
return UNKNWON;
}
ConnectivityManager connectivitymanager = (ConnectivityManager) CenterApp.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
return NOT_AVAILABLE;
}


if (networkinfo.getType() == ConnectivityManager.TYPE_WIFI) {
return WIFI;
}
String netInfo = networkinfo.getExtraInfo();
if (netInfo == null) {
return UNKNWON;
}
netInfo = netInfo.toLowerCase();
if (netInfo.equals("cmnet")) {
return CMNET;
} else if (netInfo.equals("cmwap")) {
return CMWAP;
} else if (netInfo.equals("3gnet")) {
return G3NET;
} else if (netInfo.equals("3gwap")) {
return G3WAP;
} else if (netInfo.equals("uninet")) {
return UNINET;
} else if (netInfo.equals("uniwap")) {
return UNIWAP;
} else if (netInfo.equals("ctnet")) {
return CTNET;
} else if (netInfo.equals("ctwap")) {
return CTWAP;
} else {
return MOBILE;
}
}

你可能感兴趣的:(android,网络,mobile,wifi)