我在我的设备中启用了 GPS,但我没有得到位置:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null)
{
showCurrentAddress(location);
}
}
但在这里我得到的位置是空的,我在我的代码中添加了以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
你能帮忙吗?
最佳答案
lm.getLastKnownLocation
可以返回 null
,因为这是最后一个已知位置的 getter。
尝试使用 lm.requestLocationUpdates(LocationListener)
。
关于安卓 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9868502/