Testing & troubleshooting
How to test geofences and location features on Android
To test location features, use the Android Studio emulator's Location panel (a single point, or a GPX/KML route) for fast checks, then repeat on a real device with a mock location app selected in Developer options. Drive a route across your geofence to test enter, dwell and exit, with the screen off, to catch background and Doze issues.
Loads from YouTube (youtube-nocookie.com) only when you press play.
Step by step: Test geofences on Android
- Quick check on the emulatorIn the emulator, open Extended controls › Location and set a point, or load a GPX/KML route and play it. From a terminal: adb emu geo fix <longitude> <latitude>.
- Set up a real deviceEnable Developer options and choose a mock location app under Select mock location app.
- Cross the geofence with a routePlay a route that starts outside the radius, enters, stays longer than your loitering delay, and leaves.
- Repeat with the screen offLock the phone and run the same route for at least 30 minutes to catch background limits and Doze.
- Record evidenceLog transitions in your app and capture the screen with adb shell screenrecord.
Emulator first: fast and repeatable
The Android Studio emulator has a Location panel under Extended controls: set a single point, or load a GPX/KML file and play it at different speeds. From a terminal, adb emu geo fix <longitude> <latitude> sets a point (note the order: longitude first).
Why real devices still matter
Real phones add OEM battery policies, Doze, sensor noise and the fused location provider's own timing. A geofence that fires every time on the emulator can be late or silent on a real phone with the screen off.
A practical test matrix
| Case | How to test it |
|---|---|
| Enter | Route that starts outside and crosses into the radius |
| Exit | Same route continuing out the other side |
| Dwell | Stop inside the radius longer than your loitering delay |
| Radius edge | Walk around near the boundary to see jitter-triggered transitions |
| Speed changes | Switch between walking and driving, or steer with a joystick |
| Background | Screen off for 30 minutes, battery optimization on and off |
Handle isMock() in QA builds
Locations from a mock provider carry a flag: Location.isMock() on Android 12+, isFromMockProvider() earlier. Log it in debug builds so you always know whether a test used simulated data.
Using Golub for device tests
Golub – location simulator for Android plays routes on real roads at realistic speed, imports your test GPX files, offers a floating joystick for edge cases and a walk-around mode for jitter. Routes, joystick and walk around are part of Premium.
Official reference: Android geofencing documentation.
Frequently asked questions
Why doesn't my geofence trigger on the emulator?
Geofencing needs location updates over time: a single jump may not be enough. Play a route across the boundary, make sure Google Play services is present in the image, and check that the app has background location permission.
How long before GEOFENCE_TRANSITION_DWELL fires?
After the loitering delay you set with setLoiteringDelay() on the geofence. Stay inside the radius longer than that delay in your test route.
Can I test on a real device without root?
Yes. Select a mock location app in Developer options; no root is needed.
Should production code reject mock locations?
That's a product decision. Tolerate mock locations in QA builds so testers can work, and decide separately what production should do with Location.isMock().
For testing, development and personal use. Please respect the terms of the services you use.