MyLocationOverlay not calling onProviderDisabled

2019-09-07 15:16发布

问题:

This is my code that behaves a little strange:

locationOverlay = new MyLocationOverlay(this, map) {
    @Override
    public void onLocationChanged(Location location) {
        super.onLocationChanged(location);
        updatePosition(locationOverlay.getMyLocation());
    }

    @Override
    public void onProviderDisabled(String provider) {
        if(provider.equals(LocationManager.GPS_PROVIDER))
            showNoGPSWarning();
    }
};

showNoGPSWarning shows a Dialog that asks if GPS should be enabled (like this).
Everything wokrs perfectly fine except if I start the Activity with GPS disabled and press "No" in the showNoGPSWarning() dialog. Then my App doesn't listen to GPS changes at all.

If I turn it on, nothing happens. The Google maps API doesn't try to get a GPS signal. If I turn it back off, nothing happens again, onProviderDisabled() is not called.
As I said, this is the only way that happens, if I press "Yes" in the dialog it works.
So how is my dialog affecting the behavior of my App?.

回答1:

Once again try to request for location updates when you press yes/no like below. Hope it helps.

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 2000.0f, myLocationListener);