android 基站

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        CellLocation cellLocation = telephonyManager.getCellLocation();
          //判断CellLocation类型,是gsm或cdma
          if(cellLocation!=null){
              if(cellLocation instanceof GsmCellLocation){
                  GsmCellLocation gsmCellLocation = (GsmCellLocation)cellLocation;
                  gsmCellLocation.getCid();
                  gsmCellLocation.getLac();
              }else if(cellLocation instanceof CdmaCellLocation){
                  CdmaCellLocation cdmaCellLocation = (CdmaCellLocation)cellLocation;
                  cdmaCellLocation.getBaseStationLatitude();
                  cdmaCellLocation.getBaseStationLongitude();
              }
          }

  • 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、付费专栏及课程。

余额充值