经过几天的调研以及测试,终于解决了联通2G、移动2G、电信3G的基站定位代码。团队里面只有这些机器的制式了。下面就由我来做一个详细的讲解吧。
1 相关技术内容
Google Android Api里面的TelephonyManager的管理。
联通、移动、电信不同制式在获取基站位置的代码区别。
通过基站的基本信息,通过Google Gears获取对应的GPS经纬度。
通过Google Map API根据GPS经纬度获取当前位置。

2 目前存在的几个问题
由于得到的GPS经纬度在Google Map上面显示需要偏移,这块暂时没有进行处理。
没有使用PhoneStateListener来对状态实时进行更新。
没有使用线程异步获取数据
没有使用服务的方式来实时获取数据
所以如果是商业使用的话,还需进一步修改。

3 当然本部分代码已经移植到我们的家庭卫士的项目中了,2提到的问题全部解决了。

下面我针对第一部分的四大内容进行代码注解。

1 Google Android Api里面的TelephonyManager的管理。

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


通过这个方式就可以得到TelephonyManager接口。
这个接口的源代码可以通过设置在项目里面查看,这里不具体附上了。
得到TelephonyManager后,由于针对不同的运营商,代码有所不同,所以需要判断getNetworkType()
在源代码里面有如下的类型定义
    /** Network type is unknown */public static final int NETWORK_TYPE_UNKNOWN = 0;/** Current network is GPRS */public static final int NETWORK_TYPE_GPRS = 1;/** Current network is EDGE */public static final int NETWORK_TYPE_EDGE = 2;/** Current network is UMTS */public static final int NETWORK_TYPE_UMTS = 3;/** Current network is CDMA: Either IS95A or IS95B*/public static final int NETWORK_TYPE_CDMA = 4;/** Current network is EVDO revision 0*/public static final int NETWORK_TYPE_EVDO_0 = 5;/** Current network is EVDO revision A*/public static final int NETWORK_TYPE_EVDO_A = 6;/** Current network is 1xRTT*/public static final int NETWORK_TYPE_1xRTT = 7;/** Current network is HSDPA */public static final int NETWORK_TYPE_HSDPA = 8;/** Current network is HSUPA */public static final int NETWORK_TYPE_HSUPA = 9;/** Current network is HSPA */public static final int NETWORK_TYPE_HSPA = 10;

2 联通、移动、电信不同制式在获取基站位置的代码区别。

这部分是我实际测试出来的,经过无数次的拆机,放卡,才实现了不同制式的完美实现。
代码如下:

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);int type = tm.getNetworkType();//中国电信为CTC//NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType//NETWORK_TYPE_CDMA电信2G是CDMAif (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT){}//移动2G卡 + CMCC + 2//type = NETWORK_TYPE_EDGEelse if(type == TelephonyManager.NETWORK_TYPE_EDGE){}//联通的2G经过测试 China Unicom   1 NETWORK_TYPE_GPRSelse if(type == TelephonyManager.NETWORK_TYPE_GPRS){

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

3 通过基站的基本信息,通过Google Gears获取对应的GPS经纬度。
这部分前面的两篇文章都有提到,代码参考了网友们的代码,感谢感谢。

private Location callGear(ArrayList cellID) {if (cellID == null) 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()              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;    }

4 通过Google Map API根据GPS经纬度获取当前位置。
本部分代码参考了 简单基站定位程序 ,感谢雷一兄这么好的文章。同时雷一兄的排版真的非常好看,清晰明了。

private 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;    }

5 最关键的出来了,附上代码吧。
AndroidPosition
补充一下:
在AndroidMenifest.xml里面需要加上
android.permission.INTERNET、android.permission.ACCESS_COARSE_LOCATION、android.permission.READ_PHONE_STATE权限,否则会出错。
放在Application包前面。

6 图片看一下效果吧。

7 另外在提交数据到Google Gears的时候,格式如下
发送到Google的数据格式:
02-24 18:08:20.550: E/Location send(12892): {“address_language”:”zh_CN”,”host”:”maps.google.com”,”radio_type”:”cdma”,”home_mobile_country_code”:”460″,”home_mobile_network_code”:”13965″,”cell_towers”:[{"mobile_network_code":"13965","location_area_code":11,"cell_id":1985,"age":0,"mobile_country_code":"460"}],”request_address”:true,”version”:”1.1.0″}

接收到Google的数据格式:
02-24 18:08:22.975: E/Locaiton receive(12892): {“location”:{“latitude”:43.8595097,”longitude”:125.3355736,”address”:{“country”:”中国”,”country_code”:”CN”,”region”:”吉林省”,”city”:”长春市”,”street”:”文昌路”,”street_number”:”"},”accuracy”:1815.0},”access_token”:”2:_Kpk9mOFMgyWgLai:8iWlDpBYZsp4_VxO”}

-End-

本文同发地址:http://doandroid.info/?p=1937

转载于:https://www.cnblogs.com/doandroid/archive/2012/02/24/Android_Base_Station_Location.html

Android基站定位源代码相关推荐

  1. Android 基站定位源代码

    经过几天的调研以及测试,终于解决了联通2G.移动2G.电信3G的基站定位代码.团队里面只有这些机器的制式了.下面就由我来做一个详细的讲解吧. 1 相关技术内容 Google Android Api里面 ...

  2. Android基站定位

    Android基站定位   一.通过手机信号获取基站信息 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释: MCC,Mobile Country C ...

  3. Android基站定位——单基站定位(二)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/8672856 基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息 ...

  4. Android基站定位——通过手机信号获取基站信息(一)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/8672442 基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息 ...

  5. Android基站定位——三基站(多基站)定位(三)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/8673532 这一篇基于:Android基站定位--单基站定位(二) 阐述几个概念: ...

  6. 无线基站定位服务器,android 基站定位api

    android 基站定位api 内容精选 换一换 网络告警需要有确切的发生时间.所在网元.告警名称等信息,且告警能挂载于拓扑数据的网元之上.具体告警数据格式参见API文档.设备之间需要有确定的拓扑关系 ...

  7. Android基站定位——通过手机信号获取基站信息

    基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值,想要具体地址信息的再根据经纬度值获取具体的地址信息. 一.通过手机信号获取基站信息 通过TelephonyM ...

  8. android 基站定位

    这里给大家分享下基站定位的实现,基站定位首先要通过TelephonyManager得到手机的信号信息,比如基站的国家编码,小区id等......得到这些后需要向google提供的接口提交这些参数,然后 ...

  9. 真实可行的android 基站定位代码

    来自:http://blog.csdn.net/swqqcs/article/details/7064532 大部分国产的Android定制机里不支持最简单实用的基站和WIFI定位,只能使用速度慢而耗 ...

  10. android 基站定位 api,基站定位查询接口 - whoisliang的个人空间 - OSCHINA - 中文开源技术交流社区...

    本站查询接口免费开放 所有免费接口禁止从移动设备端直接访问,请使用固定IP的服务器转发请求. 每5分钟限制查询300次,基站/WIFI/经纬度查询接口每日限制查询1000次,反向基站查询接口每日限制查 ...

最新文章

  1. php商城多属性选择,ecshop后台商品属性详解之复选属性
  2. 【Python】 Spyder
  3. 如何打开eclipse进行编写Java程序
  4. sqlite C/C++ API
  5. python引入导入自定义模块和外部文件
  6. 记录远程桌面登录者的IP和MAC
  7. mysql变量包括什么意思_什么是mysql变量?
  8. RAC11g使用数据泵导入导出报ORA-6512,ORA-25306,ORA-39079错
  9. [2018.07.17 T1] 字符串最大值
  10. html表单下拉美化教程,使用css美化html表单控件详细示例(表单美化)
  11. 软考顺口溜汇总(包括监理活动“四控、三管、一协调”等)
  12. Visual.Assist.X.V10.7.1912的Crack破解补丁
  13. 继Python之后,Go也顺利在浏览器上运行
  14. uniapp-商品详情配置
  15. linksys 打印软件_Linksys固件DD-WRT BitTorrent优化
  16. 渗透测试成功的8个关键
  17. Windows下Android源码下载
  18. frustum pointnets训练代码学习笔记——kitti_object.py
  19. Javascript实现扫雷游戏
  20. linux可以玩什么游戏_为什么我们要在Linux上玩游戏,与Icculus聊天等等

热门文章

  1. ssa/ass字幕格式全解析
  2. SQL语句:连接查询
  3. intouch与PLC之间通讯状态监测和设置
  4. 临时邮箱有什么用,推荐5个临时邮箱
  5. STM32 rtc唤醒 低功耗待机模式 避免iwdog看门狗方案
  6. android x86 4.3 root,安装好x86安卓后(凤凰系统1.04版本),出现ANDROID root@x86:/#,进不了系统...
  7. Python之文本去重(基础版)
  8. python json对比差异,更新json数据
  9. 乐优商城遇到的坑(四)之前台门户系统之Search.html
  10. python词云图绘制