Android Question Geo Fence - Android 10

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I am trying to implement a Geo fence to control the execution of some checklists and I am using the exemple in the Forum.

https://www.b4x.com/android/forum/t...ing-a-region-in-the-background.84767/#content
I have copied the GeoFenceService and used all the subs as stated in the example. I alwys get a message in the log saying "Geofence added:false"

IF I run the example all goes well and I get : Geofence added: true
I am guessing it has something to do with permissions as Android asks me if I want to allow the App to access locations and with the example I get three options :
Always
Only when the App is running
Never

With my App, I never ger the Always option.

And another thing, if I uninstall the example, change the Application Label and the the name in the package, i get the "Geofence added:false"
even when allowing the permisisons.

Can someone help, please !?

Thanks
 

roumei

Active Member
Licensed User
Geofencing requires the background location permission and that's handled more restrictive on Android 10+. For android:targetSdkVersion="29" you need to add the ACCESS_BACKGROUND_LOCATION permission in the manifest.
B4X:
AddPermission(android.permission.ACCESS_BACKGROUND_LOCATION)
More info can be found here: Create and monitor geofences | Android Developers
Please be aware that you probably still won't get the 'Allow all the time' option when you request ACCESS_FINE_LOCATION for the first time. You have to enable 'Allow all the time' in your app's settings after you installed it (Settings > App & notifications > Your app > Permissions > Location > Allow all the time). Based on the documentation, you should see the 'Allow all the time' option on Android 10 but on all my test phones this is not working. The behavior is more like Android 11.
More info about requesting background location: Request location permissions | Android Developers
If you plan to submit your app to the store you'll have to describe why your app needs background location. If you fail to do so or if Google doesn't like your use case, the app will be rejected. See Requesting access to location in the background - Play Console Help (google.com)
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
still doesn't work and the example works and it does not ask for ACCESS_BACKGROUND_LOCATION. And I am trying them both in Android 10, same tablet.
 
Upvote 0

roumei

Active Member
Licensed User
The example targets API level 26. ACCESS_BACKGROUND_LOCATION is needed for API level 29. What does your app's settings page looks like? Can you select 'Allow all the time'?
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
The example targets API level 26. ACCESS_BACKGROUND_LOCATION is needed for API level 29. What does your app's settings page looks like? Can you select 'Allow all the time'?
It works after I set the permission to "Allow all the time". Thanks
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
Now I have another issue,
Altough the service creates the GeoFence, the subs Geofence_enter and Geofence_exit at Starter, are never called, because the variable transtion always passes with -1 and does not trigger the events.
 
Last edited:
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
I noticed that in the Start_service, the intent is null, coul it be the reason to always get a -1 in the transation return value.
What Could be wrong !? This is from the example also

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
 
Upvote 0

roumei

Active Member
Licensed User
Unfortunately I can't help you here. It's probably best to post a new question for this issue.
 
Upvote 0
Top