2

I have tried several approaches to getting background updates at least every two minutes automatically from Android 11+ Location providers and none of them seem to be reliable beyond using LocationServices which affects battery life a lot. Right now, I'm trying to use Geofences but it seems to never get a trigger unless I bring up Google Maps. I'm using the Emulator and switching locations manually using extended controls. It's not that it's not working. It's not getting renewed updates. When I start the app, I get the initial trigger from

GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geofenceList);

I also have

   geofenceList.add(new Geofence.Builder()
        // Set the request ID of the geofence. This is a string to identify this
        // geofence.
        .setRequestId(String.valueOf(iAlertCount))

        .setCircularRegion(
            dLatitude,
            dLongitude,
            AppInit.LOCATION_DISTANCE
        )
        .setNotificationResponsiveness(10000)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
            Geofence.GEOFENCE_TRANSITION_EXIT)
        .build());

I am also loading a Notification Icon service using startForegroundService as I read that having a foreground service should help.

I appreciate any input on this. I'm trying to avoid using LocationServices if I can get a reliable trigger at least every two minutes.

John Smith
  • 3,493
  • 3
  • 25
  • 52
  • 1
    For location functionality I would recommend using a physical device, it's just too cumbersome to use an emulator. I would also say that using the `FusedLocationProviderClient` is probably what would work best for you to get location updates every two minutes, but it's hard to say. You can use `LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY` for the location request, which gives fairly good results without draining the battery too much. See here for an example: https://stackoverflow.com/a/44993694/4409409 – Daniel Nugent Feb 10 '22 at 20:35

0 Answers0