JMC_CMJ
JMC_CMJ

Reputation: 53

Is there a better way to accurately use geofences without writing my own location request service?

Accord to Android's documentation on geofences, the geofence should trigger when the user enters, dwells, or exits the geofence with respect to the asked request.

BUT, testing the geofence feature myself, a geofence will only trigger if an app requests the user's location. Simply using the tutorials code and moving the device into the geofence WILL NOT trigger the geofence. I have to open Google maps or some other app that will request the user's location, which I assume notifies Google play services, which then fires my geofence. Is this really how are geofences are designed? Proof that this is how geofences work I found from this answer. (Note: I am also using a Broadcast Receiver like the accepted answer in that link. The receiver does not trigger unless a location request is made within the geofence.)

I don't want to have a persistent background service asking for the user's location every 5 minutes just to trigger the broadcast reciever. I've thought of listening for system intents such as the user turning the screen on, disconnect from wifi, etc that would fire a service I would make that asks for the user's location. But shouldn't geofences trigger natively without extra need from my app or other apps to update the user's location?

Upvotes: 3

Views: 971

Answers (2)

Bruce
Bruce

Reputation: 2574

Just want to share my findings. After implementing geofence using the broadcast receiver way I found that it can be triggered without opening any location apps like Google maps given the criteria below:

  1. Test on real device, I found that my geofence doesn't get triggered on the Android emulator even after waiting one hour moving out of geofence zone. I won't trust the emulator because of lack of sim card and real wifi so I don't know what's the missing pieces.
  2. Wait 2 to 3 minutes if no other apps are requesting for location in the background. I tested this by denying location access to all other apps (or switch them to 'only allow when using app'), including the Google app on my Pixel phone.

I've been consistently getting geofence events in like 2 to 3 minutes, just like the docs says:

On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes.

For my case my app is not running on background, as it is not required for geofence to trigger if you're using broadcast receiver, I killed it on the recent apps page, or you can even use 'Force stop' in the apps info page, it doesn't matter.

Upvotes: 0

Christian
Christian

Reputation: 4641

As mentioned in this answer https://stackoverflow.com/a/54436709/1505074 you need to change the sample Code to use a Broadcast Receiver instead of a Service. This way Geofences basically work but with some caveats. You have a latency depending on API-level, manufacturer and of course the device state. Often you will get the fence event when turning the device on. To really evaluate fence events you need to have a network logging service and do more complex analyses.

The problem with your own Geofence Trigger will be that you only get a real location update in the background about 4 times per hour if you are not using a foreground service which will drain battery. Without you will only get the last location even with the location update callback if you are in the background.

Transistorsoft has created a library to handle different ways of background location tracking and you will find many approaches there, but it is paid: https://github.com/transistorsoft/background-geolocation-lt

Upvotes: 1

Related Questions