在这种情况下,我必须通过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的替代来获取位置。请更新,如有任何帮助,我们将不胜感激。
发布于 2013-01-29 15:59:38
添加此权限:manifest.xml中的ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>发布于 2015-05-01 05:05:39
从TelephonyManager来看,它可能与LTE有关:
如果设备中只有一个无线电设备,并且该无线电设备具有
连接,则此方法将返回null...
发布于 2013-01-29 15:51:35
getCellLocation()返回null,如果当前位置不是available.please,请查看
Telephonymanager
此外,请检查您的电话设置中是否存在Basebandtype类型信息。
下面的代码在我身上运行得很好,你可以测试一下
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));https://stackoverflow.com/questions/14577968
复制相似问题