private static CdmaCellLocation location = null;
private static TelephonyManager tm;
private static GetBaseLocation get;
get = new GetBaseLocation();
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
getData();
private static void getData() {
// mLocationManager.getLocation(activity, 1);
int type = tm.getNetworkType();
// 在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA,电信的3G为EVDO
// String OperatorName = tm.getNetworkOperatorName();
Location loc = null;
ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
// 中国电信为CTC
// NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType
// NETWORK_TYPE_CDMA电信2G是CDMA
if (type == TelephonyManager.NETWORK_TYPE_EVDO_A
|| type == TelephonyManager.NETWORK_TYPE_CDMA
|| type == TelephonyManager.NETWORK_TYPE_1xRTT) {
try {
location = (CdmaCellLocation) tm.getCellLocation();
if (location == null) {
return;
}
int cellIDs = location.getBaseStationId();
int networkID = location.getNetworkId();
StringBuilder nsb = new StringBuilder();
nsb.append(location.getSystemId());
CellIDInfo info = new CellIDInfo();
info.cellId = cellIDs;
info.locationAreaCode = networkID; // ok
info.mobileNetworkCode = nsb.toString();
info.mobileCountryCode = tm.getNetworkOperator()
.substring(0, 3);
info.radioType = "cdma";
CellID.add(info);
} catch (Exception e) {
Log.e("network get the latitude and longitude ocurr Exception error",
e.toString());
}
}
// 移动2G卡 + CMCC + 2
// type = NETWORK_TYPE_EDGE
else if (type == TelephonyManager.NETWORK_TYPE_EDGE) {
try {
GsmCellLocation location = (GsmCellLocation) tm
.getCellLocation();
if (location == null) {
return;
}
int cellIDs = location.getCid();
int lac = location.getLac();
CellIDInfo info = new CellIDInfo();
info.cellId = cellIDs;
info.locationAreaCode = lac;
info.mobileNetworkCode = tm.getNetworkOperator()
.substring(3, 5);
info.mobileCountryCode = tm.getNetworkOperator()
.substring(0, 3);
info.radioType = "gsm";
CellID.add(info);
} catch (Exception e) {
Log.e("network get the latitude and longitude ocurr Exception error",
e.toString());
}
}
// 联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS
else if (type == TelephonyManager.NETWORK_TYPE_GPRS) {
try {
GsmCellLocation location = (GsmCellLocation) tm
.getCellLocation();
if (location == null) {
return;
}
int cellIDs = location.getCid();
int lac = location.getLac();
CellIDInfo info = new CellIDInfo();
info.cellId = cellIDs;
info.locationAreaCode = lac;
// 经过测试,获取联通数据以下两行必须去掉,否则会出现错误,错误类型为JSON Parsing Error
// info.mobileNetworkCode =
// tm.getNetworkOperator().substring(0, 3);
// info.mobileCountryCode =
// tm.getNetworkOperator().substring(3);
info.radioType = "gsm";
CellID.add(info);
} catch (Exception e) {
Log.e("network get the latitude and longitude ocurr Exception error",
e.toString());
}
} else {
Commons.LNG = "" + 0;
Commons.LAT = "" + 0;
}
try {
loc = get.callGear(CellID);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Exception", e.getStackTrace().toString());
System.out.println(e.getStackTrace().toString());
}
if (loc != null) {
try {
Commons.LNG = "" + loc.getLongitude();
Commons.LAT = "" + loc.getLatitude();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Commons.LNG = "" + 0;
Commons.LAT = "" + 0;
}
}