onLocationChanged just called once using network p

2019-09-18 05:48发布

问题:

i have a problem using location listener and location manager.

I'm using class that i found on the Internet

public class MyLocationListener implements LocationListener
{
    public void onLocationChanged(Location location) {
        home_text.setText("Location Change");
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }
}

when i use gps provider, it works fine

locationListener = new MyLocationListener();
locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1, locationListener);

when i use network provider, the onLocationChanged just called once at the start and never called again.

locationListener = new MyLocationListener();
locationManager =    (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,1, locationListener);

although, when i open the maps using both method, it still can find my current location and update the maps.

googleMap.setMyLocationEnabled(true);

please help me...

回答1:

LocationManager.NETWORK_PROVIDER tries to locate the device using cell network towers and Wifi networks around. If neither of them change, then you won't receive any update.

Did you ask for updates and roam around (you might need to take a little walk around your place) ?

With GPS, as the signal strengthens and get more and more accurate, you will receive updates even if you don't move.