Android how to get location update in batch

I am trying to get a location update in the background using the batch version of the Fuse Location Provider Api.

I am doing this following google guidelines, requesting location updates with a callback in the PendingIntent broadcast .

For the location request, I used setMaxWaitTime to get the results of the location update as stated in the google API documentation.

My problem is that my broadcast receiver did not receive the location in batch and is still receiving one location at a time from the location service.

I also tried the new PendingIntent location update project example from Google, but the location update still only returns one location at a time.

For information I tested on Nexus 5X device as it has bag dispensing equipment .

Did I miss something?

Here's some sample code:

Creating and connecting a 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.

+3


source to share


1 answer


After testing for several days, location batching seems to work with my code.



I noticed that I need to roam with my phone for batch work and this is an accident. Some temporary location update will be delayed and the package will be delivered, and sometime it will only ship in one location.

+1


source







All Articles