I am hoping to add functionality to my application that allows me to use the ID of a triggered Geofence to select a corresponding sample(url) from an array which can then be passed to an exoplayer for streaming.
Currently I am following the geofencing api which uses a pending intent hooked up to an intentservice to manage geofence transitions etc etc.
Looking at the pending intent which is set up in the main activity can I request it to return the geofence ID upon transition? I am very new to both android and this api so any and all help would be really appreciated! Thanks very much!
If I understand your question correctly (which I may not), the Geofence ID should already be being returned when the transition event occurs.
Paraphrasing the documentation, when your PendingIntent is triggered:
You can call
GeofencingEvent.fromIntent(Intent)
to get the transition type, geofences that triggered this intent and the location that triggered the geofence transition.
From the resulting GeofencingEvent object, you can call getTriggeringGeofences()
to retrieve the list of Geofence
's that caused the transition, and then getRequestId()
on each one to retrieve the ID.
Edit Some code... this is untested (since I refuse to dance about testing Geofence boundaries!), but I'm hoping it should work. I'm assuming you already have a working Geofence setup and that it is calling your IntentService correctly. In your IntentService, you should have an onHandleIntent method:
void onHandleIntent(Intent intent) {
// retrieve the GeofencingEvent from the passed Intent
GeofencingEvent ge = GeofencingEvent.fromIntent(intent);
// get the Geofences that triggered it
List<Geofence> fences = ge.getTriggeringGeofences();
// checking there is only one fence (bad assumption!!!!), retrieve the request Id
if (fences == null || fences.size() != 1) {
return;
}
String fenceID = fences.get(0).getRequestId();
// do something funky with it...
}
You should debug or log each line to check it's working properly. Let me know if this doesn't work or wasn't what you were expecting.