首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TelephonyManager.getCellLocation()返回null?

TelephonyManager.getCellLocation()返回null?
EN

Stack Overflow用户
提问于 2013-01-29 15:44:38
回答 4查看 7.4K关注 0票数 1

在这种情况下,我必须通过GSMCellLocation获取位置,即经度和纬度,因此我尝试了以下方法:

代码语言:javascript
复制
  ...
  TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  location = (GsmCellLocation) tm.getCellLocation();
  cid = location.getCid();
  lac = location.getLac();
  ...

但是应用程序崩溃了,给了我NPE。我已经通过日志和调试进行了检查,它显示getCellLocation返回null,因此我无法获取cidlac。来自Google Docs的这个方法的签名是这样描述的:

代码语言:javascript
复制
Returns the current location of the device. Return null if current location is not available. 

现在我的问题是,如何在使用此方法之前获取当前位置,使其不为空。因为在我的情况下,我试图使用它作为GPSNetwork的替代来获取位置。请更新,如有任何帮助,我们将不胜感激。

EN

回答 4

Stack Overflow用户

发布于 2013-01-29 15:59:38

添加此权限:manifest.xml中的ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION

代码语言:javascript
复制
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
票数 2
EN

Stack Overflow用户

发布于 2015-05-01 05:05:39

TelephonyManager来看,它可能与LTE有关:

如果设备中只有一个无线电设备,并且该无线电设备具有

连接,则此方法将返回null...

票数 2
EN

Stack Overflow用户

发布于 2013-01-29 15:51:35

getCellLocation()返回null,如果当前位置不是available.please,请查看

Telephonymanager

此外,请检查您的电话设置中是否存在Basebandtype类型信息。

下面的代码在我身上运行得很好,你可以测试一下

代码语言:javascript
复制
      TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
      GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

      int cid = cellLocation.getCid();
      int lac = cellLocation.getLac();
      textGsmCellLocation.setText(cellLocation.toString());
      textCID.setText("gsm cell id: " + String.valueOf(cid));
      textLAC.setText("gsm location area code: " + String.valueOf(lac));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14577968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档