Thursday, 9 September 2021

Android geo-fence is not triggering without requestLocationUpdates

I have a pretty standard geo-fencing setup. Exactly as described in this official doco.

So there's a GeofenceBroadcastReceiver with a onReceive method that is supposed to fire when geo-fence events occur.

Here's how the geo-fence areas are declared:

val list = workAreas.map {
    Geofence.Builder().setRequestId(it.id.toString())
        .setCircularRegion(it.latLng.latitude, it.latLng.longitude, it.radius)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
        .build()
}
val request = GeofencingRequest.Builder()
    .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
    .addGeofences(list).build()
client.addGeofences(request, geofencePendingIntent).run {
    addOnSuccessListener {
    }
    addOnFailureListener {
    }
}

Ok. So the problem is that the geofencing event does not trigger when I enter a defined area. That is until I do something on the app that fires requestLocationUpdates on the fusedLocationProviderClient.

This is the snipped that does the job:

val request = LocationRequest.create().apply {
    interval = 5000
    fastestInterval = 5000
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    maxWaitTime = 1000
}

Looper.myLooper()?.let { looper ->
    fusedLocationProviderClient.requestLocationUpdates(
        request,
        locationCallback,
        looper
    )
}

This can't be right can it? You can't have location update running all time. And it wasn't how it used to work in the earlier versions (you know, with JobIntentService).

So am I missing anything here?



from Android geo-fence is not triggering without requestLocationUpdates

No comments:

Post a Comment