Недавно я создал простое приложение, чтобы получить местоположение gps и отобразить на телефоне Android. В начале я могу получить местоположение после нескольких попыток, но после того, как я переустановил файл apk, getLastKnownLocation() всегда возвращает нулевое значение.
Используемая среда разработки: - пряничный прялок API 10 2.3.6 - используется поставщик GPS
ниже приведен код i, примененный в моем проекте Android:
public class MyActivity extends MapActivity{
protected void onCreate(Bundle savedInstanceState) {
mapView = (MapView)findViewById(R.id.myTripMap);
mapController = mapView.getController();
mapView.setSatellite(false);
mapView.setStreetView(true);
mapView.displayZoomControls(false);
mapView.setBuiltInZoomControls(true);//
mapView.setClickable(true);
mapController.setZoom(14);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
updateMyCurrentLoc(location);
locationManager.requestLocationUpdates(provider, 2, 1,locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateMyCurrentLoc(location);
}
public void onProviderDisabled(String provider){
updateMyCurrentLoc(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateMyCurrentLoc(Location location) {
if (location != null) {
// other codes to get the address and display
Toast.makeText(getBaseContext(), "provider used : "+provider).show(); //testing purpose
}
else {
str = "No location found";
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
}
}
Может ли кто-нибудь предложить возможное решение для решения нулевого значения, возвращаемого getLastKnownLocation()? Любая помощь будет оценена. Спасибо.