onLocationChanged(Location location) is not workin

2019-06-14 16:26发布

问题:

This question is an exact duplicate of:

  • onLocationChanged() is never trigered by requestLocationUpdate() - Android 2 answers

I have used manager.requestLocationUpdates("gps", 1800000, 1, new LocationDetector()); method of LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); class for getting update when either location change (device's location) is at least 1 meter (almost 40 inches), OR at least 30 minutes has passed. But the update is not being received when I change device's location by 1 meter (or even more). So what can be the problem? Please tell.

Code:

public class LocationDetector implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        Log.d("GPS: ",location.getLatitude()+", "+location.getLongitude());
    }

回答1:

It's not OR, it's AND.

To get a location update with those parameters the distance needs to change at least 1 meter AND 30 minutes needs to have passed since the previous update.

Android documentation:

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed.