onLocationChanged just called once using network provider

被刻印的时光 ゝ 提交于 2019-12-13 07:31: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.



来源:https://stackoverflow.com/questions/23190540/onlocationchanged-just-called-once-using-network-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!