Android Question [SOLVED] Sanity check: Does the Android emulator work with geofences?

Sandman

Expert
Licensed User
Longtime User
EDIT: Short answer is no. Read post #22 for a bit more info and possible ugly workaround.

I'm having trouble getting the emulator working with geofences. Like, this reference code by @Erel, which works fine on my real phone, doesn't seem to work at all in the emulator. I am pretty new with working with the emulator, so I might have misunderstood something, but I can't figure out what that could be.

It would be great if somebody could help me test that it actually works:
  1. Use the example found at the geofence post
  2. Change the geofence location in the code to something you know where it is
  3. Compile as release and install into the emulator, which also automatically runs the app
  4. Make sure the log in B4A says the app successfully added the geofence
  5. Drop the app into the background
  6. Make sure to NOT launch any other apps that request location (like Google Maps etc)
  7. Open the location settings in the emulator (by pressing the ... icon in the vertical toolbar window to the right)
  8. Set the position to be near your geofence
  9. Wait a couple of minutes, did the app react and say that you entered the geofence?
  10. Set the position to be far away from the geofence
  11. Wait a couple of minutes, did the app react and say that you exited the geofence?

For the record, I'm testing this on a 6 inch emulated device with API30 API32 and Play Store. (I don't have the exact name available at the moment.) I should also mention that I have verified, using Google Maps, that when I do the above, the emulated device actually gets its position updated. So everything seems to work fine, it's just that I can't get the geofences to trigger.


UPDATE

To better explain my situation, here's what I installed from SDK Manager (because having an image with Play store would be more like the average users, I thought)

1647493741474.png



Which looks like this in the AVDManager (I've also tried getting geofences to work using the Platform_28 above, but no success)

1647493792359.png


And this is what the device looks like:

1647493706516.png



If one click this button:

1647493943637.png



A window will show, where one can control lots of stuff with the emulator, such as the location:

1647494457439.png



And, just to give an example, here's a picture showing me setting the location, and Google Maps showing where it thinks I am:

1647494771950.png



So it's possible to provide locations for the emulator just fine, it's just that I can't get geofences to trigger.
 

Attachments

  • 1647493769052.png
    1647493769052.png
    16.8 KB · Views: 302
Last edited:

aeric

Expert
Licensed User
Longtime User
That shouldn't matter to the emulator. Or did you perhaps mean your real phone?


Yep, that should work. But I'm not sure why, because if it doesn't work for one geofence, why would it work for multiple? Or perhaps you just want to make sure you have more opportunities to pass through them, I suppose...


That's an interesting post. I've searched quite a bit about this, but I hadn't found that one. It seems to point to the emulator doesn't actually get the new location until something requests a fresh location. Which is strange... I mean, it's like there's an OS service missing from it, that usually listens for location events...
I found that I don't even need to run the sample app in background. The Location only refreshes (if you already set it with the SET LOCATION button) if only we open Google Maps.

Correction: At least open the app once after restart then swipe to remove the app from recent items. The geofence service is still working.
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Turns out this is a long-standing bug in the emulator. I found this post at Stack Overflow, posted in 2010, with somewhat fresh comments from recent years.

Note that this thread is about the OS updating the location without any active polling happening. This means a typical geofence situation. It does not mean a situation where one is running an app in the foreground that is actively polling the position (like Google Maps) - for that, the built-in location settings work great.

On a sidenote, I've come up with two different approaches to make this work:
  1. Enable Developer mode in the emulated device so one can accept mock locations. Then install one of the available apps for that.
  2. Make a small app with a foreground service that every minute (or something like it) activates the GPS and requests a position. Just so the OS notices any changes, and create geofence events.
I'll go with option 2.


UPDATE

I went with the code example from the thread below, and now it works as a charm. There's something constanty polling location, which in turn cause geofence events. It's probably a bit too quick to react, compared to geofences on real devices. But at least a somewhat acceptable solution so I can use the emulator for this.
 
Last edited:
Upvote 0
Top