1

I have an android app in which I have used FusedLocationApi to get location updates of the user.

Following is the scenario: 1. I have a singleton Watcher class in which I define the pending intent to get the locations even when app is in background. Following is the code:

    private PendingIntent pendingIntent;
    private Watcher() {
      Intent locationIntent = new Intent(context, Receiver.class);
      pendingIntent = PendingIntent.getBroadcast(
                    context, 007 /*requestcode*/, locationIntent,
      PendingIntent.FLAG_UPDATE_CURRENT);
   }

Then, when I have successfully connected location services, I request for location updates:

 LocationServices.FusedLocationApi.
                    requestLocationUpdates(googleApiClient, locationRequest, pendingIntent);

Now, whenever Receiver class which extends WakefulBroadcastReceiver gets the location update, it starts the Service.class. The service class extends IntentService. Following is the code:

@Override
synchronized protected void onHandleIntent(final Intent intent) {
 if(LocationResult.hasResult(intent)) {
   LocationResult locationResult= LocationResult.extractResult(intent);
   Location location = locationResult.getLastLocation();
   printLocation(location);
 }

So my question is, given the above steps, why does the onHandleIntent gets woken up by the LocationReceiver multiple times within a period of 5 milliseconds. The lat, lng and accuracy are all the same. I have defined the

setFastestInterval(5 seconds); and
setInterval(1 minute);

Also the location accuracy is BALANCED_POWER_ACCURACY;

In my app, the

LocationServices.FusedLocationApi.
                    requestLocationUpdates(googleApiClient, locationRequest, pendingIntent);

do gets called multiple times. But according to the documentation: "Any previously registered requests that have the same PendingIntent (as defined by equals(Object)) will be replaced by this request." And I am using the same pendingIntent object to call requestLocationUpdates.

Thanks in advance.

Kanika
  • 714
  • 8
  • 24

1 Answers1

0

Hi Kanika you can set the minimum displacement before passing your location request object this will help you to get updates only when you have certain minimum displacement. you can use below method for doing same.

setSmallestDisplacement(float smallestDisplacementMeters)

Also FYI api reference link , https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest

Alok
  • 881
  • 1
  • 11
  • 18
  • Hey Alok, thanks for the response. I tried it and still got updates in few milliseconds: Here are the logs: 1) 05-17 21:33:04.654 2303-3697 Key location changed to: 37.37984540, -122.06425810 ; 2) 05-17 21:33:07.474 2303-3841/Key location changed to: 37.37984000, -122.06452450 ; 3) 05-17 21:33:15.794 2303-3866/Key location changed to: 37.37984000, -122.06452450 – Kanika May 18 '17 at 04:25
  • Try to put a larger value for the displacement say 100 or 1000 and see if still you are getting same issue ? – Alok May 18 '17 at 04:28
  • Tried it, still the same. Do you think that the pendingIntent is not getting replaced fast enough. Because I do call requestLocationUpdates() multiple times in a period of few milliseconds? – Kanika May 18 '17 at 04:35
  • Not sure but if you feel that than please share a little more code, it may be helpful . – Alok May 18 '17 at 04:40
  • I think you can remove setFastestInterval (long millis) .Most probably this is causing the issue. – Alok May 18 '17 at 04:44