Geofencing API Tutorial for Android

In this geofence tutorial, you’ll learn how to use Android’s Geofencing API to build an app with custom geofences. By Fernando Sproviero.

4.5 (12) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Removing a Geofence

If you tap on a reminder, you’ll see that you get the option to remove it. Currently, this action just removes the reminder from SharedPreferences. Now, you’ll change the remove() method so it also removes the geofence.

Open ReminderRepository.kt and modify the remove() method:

fun remove(reminder: Reminder,
           success: () -> Unit,
           failure: (error: String) -> Unit) {
  geofencingClient
      .removeGeofences(listOf(reminder.id))
      .addOnSuccessListener {
        saveAll(getAll() - reminder)
        success()
      }
      .addOnFailureListener {
        failure(GeofenceErrorMessages.getErrorString(context, it))
      }
}

If you compare this with the add() method you created earlier, you’ll notice that they’re very similar. Here, you’re just removing the geofence and reminder instead of adding them — easy!

Best Practices

When working in a mobile environment, it is always important to consider issues such as battery drain, bandwidth and connectivity. Here are a few things to keep in mind as you develop your awesome geofence-based apps:

Responsiveness

To save battery, you can use the setNotificationResponsiveness() method to decrease how frequently the app checks for an event. For example, if you set it to 180,000 milliseconds, the callback will be called every three minutes to see if a device has entered/exited a geofence. The default is zero.

Radius

Android recommends a minimum geofence radius of 100–150m. When deciding on a radius, think about the use cases for your app. Will it primarily be used while walking indoors? Driving in rural areas? Wi-Fi availability makes a big difference for device location accuracy; if there is poor or no Wi-Fi in a specific region, try using a larger radius.

Dwell

Another way to reduce your app’s power usage is to use GEOFENCE_TRANSITION_DWELL and setLoiteringDelay() when building the geofence. For example, setLoiteringDelay(360000) means that the user will have to stay within the geofence for six minutes to trigger an event. Doing this could also improve the user experience by reducing unintended notifications if a device’s location reading is not stable or if the user is traveling along the edge of a geofence.

Re-registering Your Geofence

Only re-register your geofences under the following circumstances:

  • Device reboot: You can register a BroadcastReceiver for BOOT_COMPLETED.
  • App is uninstalled and re-installed.
  • App data is cleared.
  • Google Play services data is cleared.
  • GEOFENCE_NOT_AVAILABLE error: You may get this error in the GeofenceTransitionsJobIntentService. Usually this is because the Network Location Provider is disabled.

Wi-Fi and Location Accuracy

  • Latency for triggering a geofence transition is usually two minutes on average. However, if the device has been stationary, the latency may increase to up to six minutes.
  • On most devices, the geofence service uses the Network Location Provider. This provider determines location based on cell towers and Wi-Fi access points. It works indoors and uses much less power than GPS.
  • If there’s no reliable connection to cell towers or Wi-Fi access points, geofence transitions might not trigger.
  • If Wi-Fi is disabled, the geofence service may not trigger the transitions. For Android 4.3 and later, there’s a “Wi-Fi scan only mode” that disables Wi-Fi but not the network location.

Where to Go From Here?

Congratulations! You’ve just learned the basics of geofences on Android.

You can download the final version of the project using the Download materials button at the top or bottom of this tutorial. Remember to add your Google Maps API key to the final project before you run it.

Here are some great references to learn more about the subject:

  • Official Docs: You can find them here.
  • Fence API: To be aware also of the context (walking, driving, headphones plugged in, etc.), check out more here.
  • The background processing guide will help you choose the best way for your app to perform operations in the background.
  • There’s a Google sample that you might want to check out.

Feel free to share your feedback, findings or ask any questions in the comments below or in the forums. I hope you enjoyed this tutorial on geofences!