Q:

How to implement android documentation code for comparing multiple location results?

I am developing a location aware app and when I request for location updates, I occasionally get a very old location (as if it doesn't update), or I get multiple location notifications. I started digging more into this issue and I found this blog that describes how ANDROIDS location listener works. 
In short, my interpretation is that when you requestLocationUpdates, you don't just get one location object, but you receive multiple. So then I started trying to figure out how to pick the best location object out of multiple and found an algorithm in the Android documentation (under the section: "Maintaining a Current Best Estimate")
I am confused on how to implement the block of code from that section into my own app. The block of code takes two parameters, location and currentbestlocation and compares them. 

How do you declare two location objects for the block of code to compare? (code example or pseudo code would be much appreciated!)
Is my understanding correct that locationlistener provides multiple location objects?
How do I implement GOOGLES code into mine? See below:

my code below:
public class MainActivity extends Activity {
    LocationManager lm;
    LocationListener ll;
    private Location previousLocation;

    public void onCreate(Context context, Intent intent) {    
        lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        ll = new myListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000, ll);
    }

    private class myListener implements LocationListener {      
        public void onLocationChanged(Location loc) {
        if (previousLocation == null) {
               previousLocation = loc;
            } else {
                if (isBetterLocation(loc, previousLocation)) {
                //NOTIFICATION NEW LOCATION IS BETTER
                } else {
                //NOTIFICATION PREVIOUS LOCATION IS BETTER
                }
            }
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    //GOOGLE ANDROID DOCUMENTATION CODE FOR MAINTAINING CURRENT BEST ESTIMATE
    private static final int TWO_MINUTES = 1000 * 60 * 2;

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
        // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }

    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
            return provider1.equals(provider2);
    }
}

A:

When you use the LocationManager to request location updates, it will continue giving you updates until you tell it to stop or your application is killed. This way, you can monitor the device's current location at some interval so you know when they've moved. If you only need a single location update, once you've received a location fix that is of sufficient accuracy, call locationManager.removeUpdates(listener).
If you only plan on supporting devices running Gingerbread and higher, you can also use the requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper) method.
The first thing to do is declare your location listener and register for updates. This code should get your started:
public class MyActivity extends Activity implements LocationListener {
    /** Cache the last location fix received. */
    private Location mLastLocationReceived;

    @Override
    public void onResume() {
        super.onResume();

        // Register our location listener
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, this);
    }

    @Override
    public void onPause() {
        super.onPause();

        // Unregister our location listener
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        if (mLastLocationReceived == null) {
            mLastLocationReceived = location;
        } else {
            if (isBetterLocation(location, mLastLocationReceived)) {
                // New location fix is better!
            } else {
                // New location fix is not better!
            }
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // Pass
    }

    @Override
    public void onProviderEnabled(String provider) {
        // Pass
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // Pass
    }

    private boolean isBetterLocation(Location newLocation,
            Location oldLocation) {

        // TODO: Implement the logic to determine if the new location is
        // of better quality than the old location. Your application's
        // business logic determines what this method should do.

        return false;
    }

}

Note that the isBetterLocation method always returns false and is up to you to provide the implementation. There is a good example implementation here. Also, I didn't test this code so please forgive any errors.

