Android Question Geo Fence Service - Transition Always -1

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I am trying to set a geofence service but although I can create the geofence, running the example and my App with the same code adapted to it, I can never get the events of Geofence_Enter and Geofence_Exit to run, since the transition values always return -1. I read in the google developers help that it as something to do with the intent, but I cannot figure it out what it is.

Can any google Services Expert help out ?

B4X:
Sub Service_Start (StartingIntent As Intent) 'StartingIntent  is always null

Dim GeofencingEvent As JavaObject
GeofencingEvent.InitializeStatic("com.google.android.gms.location.GeofencingEvent")
GeofencingEvent = GeofencingEvent.RunMethod("fromIntent", Array(StartingIntent))
Dim transition As Int = GeofencingEvent.RunMethod("getGeofenceTransition", Null) ' Always returns -1
Log("GeofenceService Service_Start")
Log("transition:" & transition)
If transition > 0 Then 'Because the transition is always -1 it never evaluates the following code
Dim geofences As List = GeofencingEvent.RunMethod("getTriggeringGeofences", Null)
If geofences.Size > 0 Then
Dim geofence As JavaObject = geofences.Get(0)
Dim id As String = geofence.RunMethod("getRequestId", Null)

If transition = 1 Then
CallSubDelayed2(Starter, "Geofence_Enter", id)
Else If transition = 2 Then
CallSubDelayed2(Starter, "Geofence_Exit", id)
End If
End If

End If
End Sub
 

Pedro Caldeira

Active Member
Licensed User
Longtime User
-1 means that the service was started due to an intent not related to the geofence.

You can add Log(StartingIntent) and Log(StartingIntent.ExtrasToString). It will tell you more about the intent that caused the service to start.
Hello Erel, the only service that I have already started and running is the location service, I monitor the coordinates for other purposes within the App, but the geofencingservice I only call it within that sub above. And I have only asked about the -1 because I also get it running the example in the site.

My logs return :
Intent { cmp=xevolution.vrcg.devdemov2400/.geofenceservice }
no extras
 
Last edited:
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
Actually cross a geofence border?
@Sandman, the question is the following:
When I call the service, I want the event to fire right away, because normally I start it within the geofence border, so I was expecting the service to return the Geofence_Enter event, If I started it outside, the event should be the Geofence_Exit. My question was in the sense of being able to trigger the check to evaluate my position right way and to return the correspondent value.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
My experience is that that's not how it works. You only get the events when you actually cross a border.

For your case, perhaps you could just get the gps position initially and yourself decide if you're in a zone or not. (I'm assuming the app is in the foreground for at least a couple of seconds, which might not be true.)
 
Upvote 0
Top