2

Google Play Services' Fused Location Provider Api lets you request location updates with location listeners or pending intents. I can successfully request location updates with the location listener but I have been struggling to replicate the same behaviour with pending intents. For the latter, I launch an intent service which handles the location data. What I have noticed during testing is that the location updates correspond with the interval I set in the location request. However, as time goes by the interval between updates increases tremendously even though the interval in the location request has remained constant. I have noticed this behaviour on several occasions with multiple devices. Does anyone have an idea what could be going on?

Foreground location tracking

protected void requestLocationUpdates()
{
    mIsTracking = true;
    LocationRequest locationRequest = mLocationRequests.get(mPreviousDetectedActivity.getType());
    if (locationRequest != null)
    {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }
}

@Override
public void onLocationChanged(Location location)
{
    Log.i(TAG, "onLocationChanged");
    handleLocationChanged(location);
}

Background location tracking

protected void requestLocationUpdates()
{
    LocationRequest locationRequest = mLocationRequests.get(mPreviousDetectedActivity.getType());
    if (locationRequest != null)
    {
        mResultCallabackMessage = "Request location updates ";
        PendingIntent pendingIntent = getLocationPendingIntent();
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                locationRequest, pendingIntent).setResultCallback(this);
    }
}

protected PendingIntent getLocationPendingIntent()
{
    if (mLocationPendingIntent != null) return mLocationPendingIntent;

    Intent intent = new Intent(this, LocationUpdatesIntentService.class);
    mLocationPendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return mLocationPendingIntent;
}

public class LocationUpdatesIntentService extends IntentService
{

    public LocationUpdatesIntentService()
    {
        // Use the TAG to name the worker thread.
        super(TAG);
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Bundle bundle = intent.getExtras();
        Location location = (Location)   bundle.get(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

        handleLocationUpdates(location);
    } 
}

Any help is greatly appreciated.

Wouter
  • 291
  • 4
  • 12
  • your problem is that location request increases or decreases ? In title you write decreases in text increases.. – Renetik Oct 31 '18 at 17:01

2 Answers2

0

It could be because some other application in the device must have requested for more frequent updates. Please refer this link for more details

This interval is inexact. You may not receive updates at all (if no location sources are available), or you may receive them slower than requested. You may also receive them faster than requested (if other applications are requesting location at a faster interval). The fastest rate that that you will receive updates can be controlled with setFastestInterval(long)

Shiv
  • 689
  • 8
  • 23
0

I'm having the same problem too, but as @Shiv said you should be testing different parameters:

 mClient.requestLocationUpdates(LocationRequest.create(), mLocationIntent)

try this:

 mClient.requestLocationUpdates(createLocationRequest(), mLocationIntent)

private LocationRequest createLocationRequest() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(UPDATE_INTERVAL);
    locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setMaxWaitTime(MAX_WAIT_TIME);
    return locationRequest;
}
GuilhE
  • 11,591
  • 16
  • 75
  • 116