android测试基站,android 基站定位

最近在做基站定位,移动电信的定位测试都可以。就是联通的定位不了。在这里把源码上传,希望各位帮忙找出原因。

1.[图片] android 基站定位

15164552_oRtF.png

2.[文件] GetBaseStationInfo.zip ~ 62KB     下载(1383)

3.[代码]android 基站定位

public class Get {

public static class CellIDInfo {

public int cellId;

public String mobileCountryCode;

public String mobileNetworkCode;

public int locationAreaCode;

public String radioType;

public CellIDInfo(){};

}

public Location callGear(ArrayList cellID) {

// System.out.println(cellID.get(0));

if (cellID.isEmpty()) {

return null;

}

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(

"http://www.google.com/loc/json");

JSONObject holder = new JSONObject();

try {

holder.put("version", "1.1.0");

holder.put("host", "maps.google.com");

holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);

holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);

holder.put("radio_type", cellID.get(0).radioType);

holder.put("request_address", true);

if ("460".equals(cellID.get(0).mobileCountryCode))

holder.put("address_language", "zh_CN");

else

holder.put("address_language", "en_US");

JSONObject data,current_data;

JSONArray array = new JSONArray();

current_data = new JSONObject();

current_data.put("cell_id", cellID.get(0).cellId);

current_data.put("location_area_code", cellID.get(0).locationAreaCode);

current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);

current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);

current_data.put("age", 0);

array.put(current_data);

if (cellID.size() > 2) {

for (int i = 1; i < cellID.size(); i++) {

data = new JSONObject();

data.put("cell_id", cellID.get(i).cellId);

data.put("location_area_code", cellID.get(i).locationAreaCode);

data.put("mobile_country_code", cellID.get(i).mobileCountryCode);

data.put("mobile_network_code", cellID.get(i).mobileNetworkCode);

data.put("age", 0);

array.put(data);

}

}

holder.put("cell_towers", array);

StringEntity se = new StringEntity(holder.toString());

Log.e("Location send", holder.toString());

post.setEntity(se);

HttpResponse resp = client.execute(post);

HttpEntity entity = resp.getEntity();

BufferedReader br = new BufferedReader(

new InputStreamReader(entity.getContent()));

StringBuffer sb = new StringBuffer();

String result = br.readLine();

while (result != null) {

Log.e("Locaiton receive", result);

sb.append(result);

result = br.readLine();

}

if(sb.length() <= 1)

return null;

data = new JSONObject(sb.toString());

data = (JSONObject) data.get("location");

Location loc = new Location(LocationManager.NETWORK_PROVIDER);

loc.setLatitude((Double) data.get("latitude"));

loc.setLongitude((Double) data.get("longitude"));

loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));

loc.setTime(GetUTCTime());

return loc;

} catch (JSONException e) {

return null;

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 获取地理位置

*

* @throws Exception

*/

public String getLocation(Location itude) throws Exception {

String resultString = "";

/** 这里采用get方法,直接将参数加到URL上 */

String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.getLatitude(), itude.getLongitude());

Log.i("URL", urlString);

/** 新建HttpClient */

HttpClient client = new DefaultHttpClient();

/** 采用GET方法 */

HttpGet get = new HttpGet(urlString);

try {

/** 发起GET请求并获得返回数据 */

HttpResponse response = client.execute(get);

HttpEntity entity = response.getEntity();

BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));

StringBuffer strBuff = new StringBuffer();

String result = null;

while ((result = buffReader.readLine()) != null) {

strBuff.append(result);

}

resultString = strBuff.toString();

/** 解析JSON数据,获得物理地址 */

if (resultString != null && resultString.length() > 0) {

JSONObject jsonobject = new JSONObject(resultString);

JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());

resultString = "";

for (int i = 0; i < jsonArray.length(); i++) {

resultString = jsonArray.getJSONObject(i).getString("address");

}

}

} catch (Exception e) {

throw new Exception("获取物理位置出现错误:" + e.getMessage());

} finally {

get.abort();

client = null;

}

return resultString;

}

public long GetUTCTime() {

Calendar cal = Calendar.getInstance(Locale.CHINA);

int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);

int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);

cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));

return cal.getTimeInMillis();

}

}

4.[代码][其他]代码

public class Test extends Activity {

private CdmaCellLocation location = null;

private Button btnGetInfo = null;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final Get get = new Get();

btnGetInfo = (Button)findViewById(R.id.btnGet);

btnGetInfo.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

EditText tv = (EditText)findViewById(R.id.editText1);

// TODO Auto-generated method stub

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

int type = tm.getNetworkType();

//在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA,电信的3G为EVDO

//String OperatorName = tm.getNetworkOperatorName();

Location loc = null;

ArrayList CellID = new ArrayList();

//中国电信为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)

{

location = (CdmaCellLocation) tm.getCellLocation();

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);

}

//移动2G卡 + CMCC + 2

//type = NETWORK_TYPE_EDGE

else if(type == TelephonyManager.NETWORK_TYPE_EDGE)

{

GsmCellLocation location = (GsmCellLocation)tm.getCellLocation();

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);

}

//联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS

else if(type == TelephonyManager.NETWORK_TYPE_GPRS)

{

GsmCellLocation location = (GsmCellLocation)tm.getCellLocation();

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);

}

else

{

tv.setText("Current Not Support This Type.");

}

loc = get.callGear(CellID);

//callGear(CellID);

if(loc != null)

{

try {

StringBuilder sb = new StringBuilder();

String pos = get.getLocation(loc);

sb.append("CellID:");

sb.append(CellID.get(0).cellId);

sb.append("+\n");

sb.append("home_mobile_country_code:");

sb.append(CellID.get(0).mobileCountryCode);

sb.append("++\n");

sb.append("mobileNetworkCode:");

sb.append(CellID.get(0).mobileNetworkCode);

sb.append("++\n");

sb.append("locationAreaCode:");

sb.append(CellID.get(0).locationAreaCode);

sb.append("++\n");

sb.append(pos);

tv.setText(sb.toString());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在没有 GPS 信号的情况下,可以利用基站定位获取设备的位置信息。Android 中的 TelephonyManager 类可以获取基站信息,根据基站信息可以大致确定设备所在的位置。以下是利用基站定位的示例代码: 1.在 AndroidManifest.xml 中声明权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> ``` 2.在代码中获取 TelephonyManager 对象: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); ``` 3.注册基站监听器: ```java PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override public void onCellLocationChanged(CellLocation location) { // 当基站位置发生变化时回调此方法 if (location instanceof GsmCellLocation) { GsmCellLocation gsmCellLocation = (GsmCellLocation) location; int cid = gsmCellLocation.getCid(); // 基站编号 int lac = gsmCellLocation.getLac(); // 位置区域码 // 根据基站信息获取位置信息 // ... } else if (location instanceof CdmaCellLocation) { CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) location; int baseStationId = cdmaCellLocation.getBaseStationId(); // 基站编号 int networkId = cdmaCellLocation.getNetworkId(); // 网络编号 int systemId = cdmaCellLocation.getSystemId(); // 系统编号 // 根据基站信息获取位置信息 // ... } } }; telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION); ``` 4.根据基站信息获取位置信息: ```java int cid = ...; // 基站编号 int lac = ...; // 位置区域码 Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(cid, lac, 1); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String country = address.getCountryName(); // 国家 String adminArea = address.getAdminArea(); // 省份 String locality = address.getLocality(); // 城市 String subLocality = address.getSubLocality(); // 区县 String thoroughfare = address.getThoroughfare(); // 道路 String subThoroughfare = address.getSubThoroughfare(); // 门牌号 String addressLine = address.getAddressLine(0); // 具体地址 // ... } ``` 需要注意的是,基站定位的精度通常比 GPS 低,而且需要访问网络获取基站信息,因此获取位置信息的速度比 GPS 慢。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值