A not-so-costly Google Location Listener implementation for Android apps

You need to, sometimes, get a very quick GPS position, even if not a very fine estimation, and then after that you can rely on a more relaxed location updates. This below is a way to speed up the initial requests and then later switched to “relaxed” mode.

Up top, you can have these variables.

private LocationClient locationClient = null;
private longinitialInterval = 2000; // 2 seconds
private long usualInterval = 10000; // 10 seconds
private int num Updates = 0;
LocationRequest locReq = new LocationRequest();

Then, on initial connection, set the request time to initialInterval (2 seconds).

@Override
public void onConnected(Bundle connectionHint){
System.out.println("Success connected to GooglePlayServices");
locReq.setInterval(initialInterval);
locationClient.requestLocationUpdates(locReq, this);
}

After a certain number of updates, change to usualInterval, which is 10 seconds (can be more).

@Override
public void onLocationChanged(Location location) {
LocationCache.getInstance().setPosition(getPos(location));
System.out.println("loc: " + location.getLatitude() 
+ "/" + location.getLongitude());

if (numUpdates<30){
locReq.setInterval(usualInterval);
locationClient.requestLocationUpdates(locReq, this);
numUpdates = numUpdates + 1;
}

}