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.