sodcalculator

10 Tech How-To

Low Power Android Geofencing for Background Apps

Guide 10-28156 · Updated · Step by step

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

  1. Add the dependency – In your app’s build.gradle file, add the Google Play services location library: implementation 'com.google.android.gms:play-services-location:21.0.1'. Sync your project.
  2. Request location permission – Add the following permissions to your AndroidManifest.xml: ACCESS_FINE_LOCATION and ACCESS_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.
  3. Create a GeofencingClient – In your main activity or a dedicated manager class, instantiate GeofencingClient using LocationServices.getGeofencingClient(context).
  4. Define the geofence – Build a Geofence object with the desired latitude, longitude, radius (in meters), and transition types (e.g., GEOFENCE_TRANSITION_ENTER and GEOFENCE_TRANSITION_EXIT). Set an expiration duration (for example, 12 hours) or use GEOFENCE_REMOVE_EXPIRATION for indefinite.
  5. Create a PendingIntent – Create an explicit intent that starts a broadcast receiver or a foreground service. For background delivery, use a BroadcastReceiver and wrap it in a PendingIntent with the FLAG_UPDATE_CURRENT flag. Example: Intent intent = new Intent(context, GeofenceReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
  6. Add the geofence – Call geofencingClient.addGeofences(request, pendingIntent) where request is a GeofencingRequest built with the geofence and an initialization trigger (optional). Add a success/failure listener to handle errors.
  7. Handle geofence events – In your broadcast receiver’s onReceive method, extract the geofence transition from the intent using GeofencingEvent.fromIntent(intent). Check the transition type and perform your desired action (e.g., send a notification).
  8. 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 Activity as the receiver – geofence events require a BroadcastReceiver or 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_STATE permission – 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 setInitialTrigger with GEOFENCE_INITIAL_TRIGGER_ENTER if 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 GeofencingEvent null 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

The whole Tech How-To bed →