Search code examples
androidgeolocationlocationgoogle-play-servicesfusedlocationproviderapi

Android how to get location update in batch


I am trying to get location update in background with the batched version of Fused Location Provider Api.

I am doing this by following Google guidelines by requesting a location updates with a callback on a broadcast PendingIntent.

For the location request I used setMaxWaitTime to get the location update results in batched as specific by Google API documentation.

My problem is that my broadcast receiver didn’t get the location in batch and still get one location at a time from location service.

I also tried a new PendingIntent location update sample project from google but the location update still return only one location at a time.

For information I tested on a Nexus 5X device as it has a batching sensor hardware.

Am I missing something ?

Here is an example of the code :

Build and connected Google Api Client

private static final long UPDATE_INTERVAL = 60000; // Every 60 seconds.
private static final long FASTEST_UPDATE_INTERVAL = 30000; // Every 30 seconds
private static final long MAX_WAIT_TIME = 60000 * 5; // Batch every 5 minutes.

public void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mLocationRequest.setMaxWaitTime(MAX_WAIT_TIME);

    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle connectionHint) {
    try {
        // Create a pending intent to listening on location service when the update become available
        Intent mIntent = new Intent(context, LocationReceiver.class);
        mPendingIntent = PendingIntent.getBroadcast(context, 42, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        // Permission check before launching location services
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mPendingIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

BroadcastReceiver class

public class LocationReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    try {

        // Check if intent has location result
        if (LocationResult.hasResult(intent)) {
            // Get our location from the LocationResult
            LocationResult locationResult = LocationResult.extractResult(intent);
            if (locationResult != null) {

                List<Location> locations = locationResult.getLocations();

                for (Location location : locations) {
                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.FRANCE);
                    String dateString = formatter.format(new java.util.Date(location.getTime()));

                    Log.d(TAG, "LOCATION: lat:" + location.getLatitude() + " long:" + location.getLongitude() +
                            " radius:" + location.getAccuracy() + " date:" + dateString);
                }

            } else {
                Log.d(TAG, "Location is null");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Thanks for any help.


Solution

  • After testing for a few days it seems that location batching is working with my code.

    I noticed that I have to move with my phone for batching to work and it is random. Some time location update will be delayed and delivered a batch and sometime it will deliver just one location.