This guide shows you how to set up geofencing on Android so your app can detect when a user enters or leaves a location, even when it’s in the background, without draining the battery. You’ll use the system’s built-in Geofencing API, which batches location updates and triggers intents only when a boundary is crossed. You’ll need Android Studio, a device or emulator with Google Play services, and a basic understanding of Kotlin and Android app structure.
What You’ll Need
- Android Studio (latest stable version)
- A physical Android device or emulator with Google Play services
- Basic knowledge of Kotlin and Android app development
- Google Play services location library added to your project
Step-by-Step
- Add the dependency – In your app’s
build.gradlefile, add the Google Play services location library:implementation 'com.google.android.gms:play-services-location:21.0.1'. Sync your project. - Request location permission – Add the following permissions to your
AndroidManifest.xml:ACCESS_FINE_LOCATIONandACCESS_BACKGROUND_LOCATION. The background permission is required for geofence events to be delivered when the app is in the background. For Android 10+ you must also request them at runtime; for Android 11+ the background permission must be requested separately after the foreground permission is granted. - Create a GeofencingClient – In your main activity or a dedicated manager class, instantiate
GeofencingClientusingLocationServices.getGeofencingClient(context). - Define the geofence – Build a
Geofenceobject with the desired latitude, longitude, radius (in meters), and transition types (e.g.,GEOFENCE_TRANSITION_ENTERandGEOFENCE_TRANSITION_EXIT). Set an expiration duration (for example, 12 hours) or useGEOFENCE_REMOVE_EXPIRATIONfor indefinite. - Create a PendingIntent – Create an explicit intent that starts a broadcast receiver or a foreground service. For background delivery, use a
BroadcastReceiverand wrap it in aPendingIntentwith theFLAG_UPDATE_CURRENTflag. Example:Intent intent = new Intent(context, GeofenceReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); - Add the geofence – Call
geofencingClient.addGeofences(request, pendingIntent)whererequestis aGeofencingRequestbuilt with the geofence and an initialization trigger (optional). Add a success/failure listener to handle errors. - Handle geofence events – In your broadcast receiver’s
onReceivemethod, extract the geofence transition from the intent usingGeofencingEvent.fromIntent(intent). Check the transition type and perform your desired action (e.g., send a notification). - Remove geofences when done – To save battery, remove geofences when they are no longer needed using
geofencingClient.removeGeofences(pendingIntent).
Common Mistakes
- Forgetting to request
ACCESS_BACKGROUND_LOCATION– without it, geofence events won’t fire when the app is in the background on Android 10+. - Using an
Activityas the receiver – geofence events require aBroadcastReceiveror a foreground service; an activity will not be started in the background. - Setting too small a radius – Android recommends a minimum of 100 meters; smaller radii may not trigger reliably and can cause battery drain.
- Not handling the case where Google Play services is unavailable – always check for availability before adding geofences.
- Forgetting to add the
ACCESS_NETWORK_STATEpermission – it’s needed for the geofencing service to determine network state.
Tips
- Use a foreground service with a low-priority notification for critical geofence monitoring, but for most cases a broadcast receiver is sufficient and more battery-friendly.
- Set a reasonable expiration duration – geofences that never expire consume more resources.
- Batch multiple geofences into one request to reduce overhead.
- Test on a real device – emulators often have location simulation quirks that can mislead you.
- Use the
setInitialTriggerwithGEOFENCE_INITIAL_TRIGGER_ENTERif you want to know immediately if the user is already inside the geofence.
Troubleshooting
- Geofence events never fire – Check that all permissions are granted, especially background location. Verify that the device’s location is on and set to high accuracy. Ensure the geofence radius is at least 100 meters.
- Events fire but app crashes – Check for a missing
GeofencingEventnull check. The intent may be null if the broadcast was not sent correctly. - Battery drains quickly – Reduce the number of geofences, increase the radius, or shorten the expiration duration. Also ensure you are not continuously updating location in the background.
- Geofence added but never triggers on exit – The device must be inside the geofence when it is added, or you must use the initial trigger. Also, exit transitions may be delayed; Android batches them.
- Google Play services error – Make sure your device has the latest Google Play services installed. In the emulator, use a system image that includes Google APIs.
Frequently Asked Questions
Q: How accurate is geofencing?
A: Accuracy depends on the device’s location provider, typically within 50-100 meters. The system uses a combination of GPS, Wi-Fi, and cell towers to determine location.
Q: Can I use geofencing without the background location permission?
A: No, for background delivery on Android 10 and higher, you must request and be granted ACCESS_BACKGROUND_LOCATION. Without it, events will only be delivered while the app is in the foreground.
Q: What is the minimum geofence radius?
A: Android officially recommends a minimum of 100 meters. Smaller radii may not trigger reliably and can increase battery usage.
Q: How many geofences can I monitor at once?
A: The system limit is 100 geofences per app. Exceeding this will cause an error when adding.
Q: What happens when the device is rebooted?
A: Geofences are not persisted across reboots. You need to re-register them, for example, by using a BOOT_COMPLETED broadcast receiver.
Q: Can I use geofencing for indoor locations?
A: Geofencing is not designed for indoor use; GPS signals are weak indoors, and Wi-Fi-based location may be inconsistent. Consider using beacons or other indoor positioning methods.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Google Chrome Helper: What It Is & How to Disable
- Fix 0x80070422 on Windows Server (Step-by-Step)
- How to Troubleshoot a Malfunctioning Driver on Windows
- How to Create Folders in Microsoft Outlook
- Fix Status Range List Conflict 0xc0000282 in Windows
- Fix Software Not Uninstalling on Windows 10