Pourquoi mon OnLocationChanged() n'obtiennent jamais appelé?

Ce que je suis en train de faire:

Je suis en train de développer une application qui nécessite seulement l'emplacement de l'utilisateur au démarrage d'une activité. De sorte que lorsque l'utilisateur est à l'intérieur de l'activité, l'emplacement est mis à jour que ce soit par réseau ou GPS. En conséquence, l'utilisateur peut choisir une piscine intérieure une carte.

Qu'est-ce que mon problème:

Cependant, je trouve que l'application est toujours à l'aide de l'histoire de l'emplacement, et de ne jamais les mises à jour de l'emplacement. Je présume qu'il doit être quelque chose de mal avec mon

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

Mais je ne sais pas où est le problème.

Liées extrait de code:

Dans mon Activity, j'ai:

    locationDetector = new LocationDetector(MapSelectionActivity.this);
    //try to get the current location
    if (locationDetector.checkLocationServiceAvailability()) {
        location = locationDetector.getLocation();
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
        Log.d("MapSelectionActivity", latitude + " " + longitude);
        //locationDetector.stopLocalization(); //stop the localization to save the energy
    } else { //if no location service, requires the user to turn GPS on
        locationDetector.showSettingsAlert();
    }

Mon LocationDetector classe est comme suit:

public final class LocationDetector implements LocationListener {
private final Context mContext;
private boolean isNetworkEnabled = false;
private boolean isGPSEnabled = false;
private boolean canGetLocation = false;
private Location location;
private String providerUsed;
//The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; //0 meters
//The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = (long) (1000 * 60 * 0.5); //0.5 minute
//Declaring a Location Manager
protected LocationManager locationManager;
//constructor
public LocationDetector(Context context) {
this.mContext = context;
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
//NOTE call  checkLocationServiceAvailability(); first before calling this!
public Location getLocation() {
//I SUSPECT SOMETHING IS WRONG HERE
if (isNetworkEnabled) { //use network
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("LocationDetector", "Using Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
providerUsed = "Network";
} else if (isGPSEnabled) { //use GPS
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("LocationDetector", "Using GPS");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
providerUsed = "GPS";
} else { //neither the network nor the GPS is on
providerUsed = null;
Toast.makeText(mContext, "Location service is unavaliable", Toast.LENGTH_SHORT).show();
}
return location;
}
//call this to restart requesting the detecting
public void startLocalization() {
if (locationManager != null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
}
//call this to stop the detecting to save power
public void stopLocalization() {
if (locationManager != null) {
locationManager.removeUpdates(LocationDetector.this);
}
}
//check location service availability
public boolean checkLocationServiceAvailability() {
//check GPS on or off
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//check Internet access
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
isNetworkEnabled = true;
} else {
isNetworkEnabled = false;
}
if (isGPSEnabled || isNetworkEnabled) {
canGetLocation = true;
} else {
canGetLocation = false;
}
return canGetLocation;
}
public String getLocationProvider() {
return providerUsed;
}
//show alert dialog to direct the users to the settings
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
//make it uncancellable
alertDialog.setCancelable(false);
//Setting Dialog Title
alertDialog.setTitle("Forgot to turn GPS on?");
//Setting Dialog Message
alertDialog.setMessage("Currently there is no Internet access.\n\nLocalization requires GPS when Internet is unavailiable.\n\nDo you want to enable GPS so as to proceed?");
//On pressing Settings button
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
Toast.makeText(mContext, "After enabling GPS, press the physical 'Back' button to return", Toast.LENGTH_LONG).show();
}
});
//on pressing cancel button
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Toast.makeText(mContext, "No location service, please choose map manually", Toast.LENGTH_LONG).show();
}
});
//Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location _location) {
//IT NEVER GETS CALLED
location = _location;
//update the text view
MapSelectionActivity.coordinatesTextView.setText("(" + Math.round(location.getLatitude() * 1000) / 1000.0 + ", " + Math.round(location.getLongitude() * 1000) / 1000.0 + ")");
//update the marker on Google Maps
MapSelectionActivity.googleMap.clear();
MapSelectionActivity.googleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("I am here!"));
MapSelectionActivity.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 15)); //15 is the approporiate zooming level
//re-suggest the map
int recommendedMapSequenceNumber = MapSelectionActivity.mapDatabase.getMapSequenceNumber(location.getLatitude(), location.getLongitude());
MapSelectionActivity.recommendedMapTextView.setTextColor(Color.parseColor("red"));
if (recommendedMapSequenceNumber == -1) { //the so-called nearest is still too far
Toast.makeText(mContext, "Please manually select one to proceed", Toast.LENGTH_LONG).show();
MapSelectionActivity.recommendedMapTextView.setText("No recommended maps");
MapSelectionActivity.autoSelectButton.setEnabled(false);
} else { //suggest a map
Toast.makeText(mContext, "One suitable map found", Toast.LENGTH_SHORT).show();
MapSelectionActivity.recommendedMapTextView.setText(MapSelectionActivity.mapDatabase.getMapName(recommendedMapSequenceNumber));
}
Toast.makeText(mContext, "New location detected", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

Je ne vois jamais de Toast dans onLocationChanged(), ce qui signifie qu'il n'est jamais appelé!
Aussi à partir de la carte, je peux voir la situation n'est pas mis à jour.

(1000 * 60 * 0.5) ce sera effectivement en 30 secondes, vous avez besoin de le changer pour (1000 * 60 * 5) pour 5 minutes d'intervalle, Cependant ce n'est pas le véritable problème.
Avez-vous donné les autorisations nécessaires dans le Manifeste ?
Ces 5 minutes, je pense sont relatives à la GPS_PROVIDER, parce qu'ils sont tout à fait OK pour le NETWORK_PROVIDER?
ok bon, maintenant commenter ce code if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } et essayer de l'exécution de votre code une fois de plus.
Vous avez peut-être l'emplacement rafraîchi par la GPS_PROVIDER, mais puisque vous le donner 30 secondes pour le refresh - il n'est jamais assez, et c'est pourquoi il ne vient pas de l'actualiser. De l'autre côté, vous êtes getLastKnownLocation(LocationManager.NETWORK_PROVIDER), qui n'est jamais actualisée, depuis la préférence fournisseur de GPS - droit?

OriginalL'auteur Sibbs Gambling | 2013-07-23