我处于一种情况,我必须通过 GSMCellLocation
获取位置,即经度和纬度,因此我正在尝试以下方式:
...
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();
cid = location.getCid();
lac = location.getLac();
...
但应用程序崩溃并给我 NPE
。我通过日志和调试检查了它,它显示 getCellLocation
返回 null
因此我无法获得 cid
和 lac
。 Google Docs 中此方法的签名是这样表述的:
Returns the current location of the device. Return null if current location is not available.
现在我的问题是,如何在使用此方法之前获取当前位置,使其不为空。因为在我的情况下,我试图用它代替 GPS
和 Network
来获取位置。请更新,如有任何帮助,我们将不胜感激。
最佳答案
添加此权限:ACCESS_COARSE_LOCATION
或 ACCESS_FINE_LOCATION
在您的 manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
关于android - TelephonyManager.getCellLocation() 返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577968/