我有一个简单的位置管理器,通常工作,但是当Android设备已被关闭,然后重新打开,android位置管理器返回空,即使我有它请求更新。我知道getLastKnownLocation可以返回null,但我相信我正在处理我的代码。所有建议赞赏。Android位置管理返回NULL
显然lon = location.getLongitude();正在崩溃。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
// request location update!!
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lon = location.getLongitude();
lat = location.getLatitude();
}
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Get last known location
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Update if not null
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
//Request update as location manager can return null otherwise
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lat = location.getLatitude();
lon = location.getLongitude();
}
}
这是一个愚蠢的问题,但仍然有添加的权限<使用许可权的android:NAME = “android.permission.ACCESS_FINE_LOCATION”/> <使用许可权的android:NAME =“机器人。 permission.INTERNET“/> – Soham
我有,是的! :D – ThemuRR