LocationManager inside a fragment

5.4k Views Asked by At

I'm trying to convert an Activity which uses LocationListener to a Fragment, using LocationManager like this:

protected LocationManager locationManager;

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    locationManager = (LocationManager) getActivity().getSystemService(
                Context.LOCATION_SERVICE);
(...)
}

I'm having some problems with these methods (can't get rid of the errors):

if (location != null) {
    // location known:
    onLocationChanged(location); // Error here
}


boolean startLocationUpdates() {
    boolean result = false;
    for (final String provider : locationManager.getProviders(true)) {
        locationManager.requestLocationUpdates(provider, 2 * 1000, 0.0f,
                this); // Error here
        result = true;
    }
    return result;
}

@Override
public void onPause() {
    super.onPause();
    locationManager.removeUpdates(this); // Error here
    savePrefs();
}

How can I use these methods inside a fragment?

1

There are 1 best solutions below

0
On

You're mixing up the relationship between the LocationManager and the LocationListener that actually receives the updates. For example, onLocationChanged, where you indicate an error, is really a callback that the LocationListener you assign to that LocationManager uses to pass a new location object to your activity/fragment. It's not something you call yourself.

The way your LocationListener and LocationManager would interact is something like this. You'd have an instance of both within your fragment. onActivityCreated you would initilize both the LocationManager and LocationListener with the LocationListener implementing all the required callbacks below.

 locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            GeoPoint userLoc = processNewLocation(location);
            if(userLoc != null){
                Log.d("USERLOC", userLoc.toString());
                                    //do something with the location    
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

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

        }

    };

Your LocationListner would then call onLocationChanged each time the locatino change met whatever critera you assigned at creation. Check the android docs for LocationManager and LocationListener for more details.