1

I'm triggering a BoradcastReceiver when receiving a location update

 PendingIntent pendingIntent = PendingIntent
          .getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT);
      LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient,
          mLocationRequest, pendingIntent); 

And my Receiever

 public static class LocationReceiver extends BroadcastReceiver {

@Override
    public void onReceive(Context context, Intent intent) {
      boolean hasLocation = LocationResult.hasResult(intent);
    }
}

If I run the above code eveything works fine hasLocation is always true, perfect.

But If I wish to pass some variable to the Receiver so I do:

Intent intent = ..
intent.putExtra("test", "hello");
PendingIntent pendingIntent = PendingIntent
              .getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Bit now in the reciever LocationResult.hasResult(intent); is always false

Is this a bug? Is there a workaround to this? How can I pass variable to the reciever?

Johny19
  • 5,364
  • 14
  • 61
  • 99

1 Answers1

1

I found your question while googling. I will share my solution in case anyone else finds this question like me.

First, this is my situation, which is similar to yours:

requestLocationUpdates() stores the location data in the mExtras field of the intent. For some reason, if I add another Extra to the Intent using Intent.putExtra(), the location data is not added. So onHandleIntent() is called, but the intent is missing the location data. If I don't add any Extras, then the location data comes through and everything is fine.

My workaround:

I used Intent.addCategory() and getCategory() to do the exact same thing as putExtra("myExtraName", String). If you want to pass other data types, convert them to a string then parse them in onHandleIntent().

My environment:

I am using Play Services version 11.0.4 and FusedLocationProviderClient, since FusedLocationProviderApi has been deprecated recently. The FusedLocationProviderClient.requestLocationUpdates() documentation doesn't seem to address this.

paperduck
  • 1,175
  • 1
  • 16
  • 26
  • The documentation doesn't address this. But it still has the same Problem it seems. I just ran into it and found this here. – Patrick Boos Sep 26 '17 at 00:41
  • 1
    There is just one problem (or difference) with using addCategory. And that is when calling "removeLocationUpdates". With extras, they are ignored. With category, the categories have to be the same. So you can only cancel if you create the exact same PendingIntent with the same categories. – Patrick Boos Sep 26 '17 at 00:48