Using Geofences with a BroadcastReceiver on Android

Sebastian Schäf
3 min readJul 2, 2019

If you want to add a location-based behaviour to your App and not ruin your user’s battery life, Geofences are usually the way to go. The idea is simple: Tell the Android system a location and it will let you know, when the user has reached it. You might suspect, that there’s a little bit more to it.

We will briefly walk through the whole process, so I recommend you to read through the official documentation on working with Geofences, if you want to understand them in more detail.

Let’s start with our use-case: We’re developing a to-do app (like everyone else — in my case it’s SwiftTask) and want to add location-based reminders to it. Our users should be able to formulate a task like that:

Remind me of buying milk the next time I’m at the supermarket.

Since we can read the people’s minds, we already know where the user’s supermarkets are located and how they are called. So, we stored these informations already in Location objects that each hold a name and latLng field. We put all of them inside the Collection locations, which is our input parameter:

val locations = arrayOf(location0, location1, ..)

0. Add the dependencies and permissions

Place this line in your build.gradle of the corresponding module. Usually, this is simply the “app module”.

And the location permissions inside your AndroidManifest.xml. Don’t forget to request them before trying to register the Geofences.

1. Build the Geofences

We’re giving the geofences an unique ID, so we can later identify which geofence has been triggered. The other parameters are pretty self-explanatory:

  • Remind us for the rest of our lives to buy milk by keeping the Geofence active forever — or, well, until we reboot the phone.
  • The Geofence region is a circle around our point, defined by the latitude and longitude, with a radius of GEOFENCE_RADIUS meters.
  • The system should notify us, when the user enters the Geofence.

2. Build the Geofencing Request

The Request simply contains our list of Geofences we want to have approved. Additionally, we set an initial trigger:

  • INITAL_TRIGGER_ENTER notifies us the first time, when the user enters one of our Geofences
  • INITIAL_TRIGGER_DWELL notifies us only after the user stayed for a while inside one of our Geofences
  • INITIAL_TRIGGER_EXIT— Make a guess.

3. Build the PendingIntent

Our intent is to callGeofenceReceiver(our BroadcastReceiver), once the user stepped inside a Geofence.

4. Throw everything together and actually request it.

5. Finally, let’s create the BroadcastReceiver

When our user now walks into a supermarket, this BroadcastReceiver gets called. We then handle the different cases by displaying an occurred error or, hopefully more often, showing a notification:

Don’t forget to remove the Geofences when you don’t need them anymore withgeofencingClient.removeGeofences(requestIds). This is important, because the device only allows you to store 100 Geofences per user for your App!

However, you need to renew them after each Reboot and for other reasons, which you can find in the documentation.

--

--