Code example for LocationManager: Getting continuous location updates

Methods: getBestProviderisProviderEnabledremoveUpdatesrequestLocationUpdates

21
        ifGPSregisterOneShotNetworkUpdate(); 
        locationUpdateManager.removePassiveLocationUpdates();
    } 
 
    private void ifGPSregisterOneShotNetworkUpdate() { 
        String bestEnabledProvider = locationManager.getBestProvider(criteria, true);
        if (bestEnabledProvider != null && LocationManager.GPS_PROVIDER.equals(bestEnabledProvider)) {
            locationManager.removeUpdates(oneShotNetworkLocationListener);
            if (isNetworkProviderEnabled()) { 
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, oneShotNetworkLocationListener);
            } 
        } 
    } 
 
    @Override 
    public void stopLocationUpdates() { 
        if (!settings.shouldUpdateLocation()) {
            return; 
        } 
        listeningForActiveLocationUpdates = false;
        stopListeningForLocationUpdates(); 
    } 
 
    private void stopListeningForLocationUpdates() { 
        locationUpdateManager.removeActiveLocationUpdates();
        locationUpdateManager.startPassiveLocationUpdates();
        locationManager.removeUpdates(oneShotNetworkLocationListener);
    } 
 
    @Override 
    public Location getLocation() {
        return currentLocation;
    } 
 
    @Override 
    public void setLocation(Location location) {
        if (locationAccuracy.isBetterLocation(location, currentLocation)) {
            currentLocation = location;
            sendLocationUpdateBroadcast(); 
        } 
    } 
 
    @Override 
    public void providerStatusChanged() { 
        if (listeningForActiveLocationUpdates) {
            try { 
                locationUpdateManager.removeActiveLocationUpdates();
                startListeningForLocationUpdates(); 
            } catch (NoProviderAvailable npa) {
                //We cant listen for updates if no provider is enabled 
            } 
        } 
    } 
 
    @Override 
    public boolean isNetworkProviderEnabled() { 
        return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } 
 
    @Override 
    public boolean isGpsProviderEnabled() { 
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);