Android的基站定位实现

 一、基站就是:当我们手机开机时,手机会自动向信号最强的无线通讯联系,注册信息,这个通讯就是我们所说的基站。

        每个基站都有自己的ID,我们通过这个基站的ID能够找到基站的位置(经纬度),而国内城市的基站密度可以达到500米以下或者更低,所以能够答题确定我们的位置,如果做一些大概的定位,则可以选用。如果是要精确的定位就用GPS定位,但是GPS定位有个缺点,在室内无法定位,这样的情况可以考虑使用WIFI的定位,当然这些都是不是本文要表述的内容,接下来看看如何实现基站的定位。

        二、准备的工具和工作:
        TelephonyManager类:主要提供了手机通讯相关的状态和信息。包括手机SIM卡的状态和信息、电信运营商网络的状态及手机用户的信息。
        GsmCellLocation类:转载 TelephonyManager中get到的信息。
        JSONObject、JSONArray:把获得的信息组建成json数据,以便存储和解析。
        需要相关手机状态、联网的相关系统权限(uses-permission)。
        
        三、具体实现:
        1.获得基站信息
        TelephonyManager  mTManager  =  ( TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);

       
 //对外提供的方法,底下的private是封装了的
 public String getCellTowersInfo(){
getMobileInfo();  //获得手机的一些信息
JSONObject object = getJSONObjectInfo();  //获得手机的数据封装的jsonobject对象
String result = getTelephoneInfo(object); //获得手机网络请求后响应的结果
return result;
}
private void getMobileInfo()
{

GsmCellLocation gcl = (GsmCellLocation) mTManager.getCellLocation();
int cellId = gcl.getCid();  //手机的ID号
int lac = gcl.getLac(); // location area code
// mobile country code, CDMA的话信息不是很准确,要用getPhoneType()来获得
int mcc = Integer.valueOf(mTManager.getNetworkOperator()
.substring(0, 3));
// mobile network code, CDMA的话信息不是很准确,要用getPhoneType()来获得
int mnc = Integer.valueOf(mTManager.getNetworkOperator()
.substring(3, 5));

setCellId(cellId);
setLac(lac);
setMcc(mcc);
setMnc(mnc);
}
private JSONObject getJSONObjectInfo(){
JSONObject object = null;
try
{
object = new JSONObject();
object.put("version", "1.1.0");
object.put("host", "maps.google.com");
object.put("request_address", true);
if(mcc == 460){
object.put("address_language", "zh_CN");
}else{
object.put("address_language", "en_US");
}
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
data.put("cellId", cellId);
data.put("location_area_code", lac);
data.put("mobile_country_code", mcc);
data.put("mobile_network_code", mnc);
array.put(data);
object.put("cell_towers", array);
} catch (JSONException e)
{
e.printStackTrace();
}
return object;
}
private String getTelephoneInfo(JSONObject object){
//创建连接,发送请求并接受回应
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
StringEntity entity = new StringEntity(object.toString());
post.setEntity(entity);
HttpResponse response = client.execute(post);
BufferedReader br = null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  //请求响应成功value是200
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));;
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while(result != null){
sb.append(result);
result = br.readLine();
}
br.close();
return sb.toString();
}else{
return "网络请求失败";
}
}catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}

}

        2. 最后别忘了,在 AndroidMenifest.xml里添加权限:
        < uses-permission android:=" android.permission.INTERNET"/><!-- 访问网络 -->
        < uses-permission  android:=" android.permission.ACCESS_COARSE_LOCATION "/><!-- 访问本地位置  -- >
        < uses-permission  android:=" android.permission.READ_PHONE_STATE "/><!-- 读取手机状态  -- >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值