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).

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

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

public void onLocationChanged(Location location) {
System.out.println("loc: " + location.getLatitude() 
+ "/" + location.getLongitude());

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