Sometimes users want to use a particular app when they are in a specific type of location, such as an airport, or a supermarket. However, users have to navigate to that app and then take a specific action within it while they are near the area of interest.
The geofencing API allows you to define perimeters, also referred to as geofences, which surround the areas of interest. Your app gets a notification when the device crosses a geofence, which allows you to provide a useful experience when users are in the vicinity.
For example, an airline app can define a geofence around an airport when a flight reservation is near boarding time. When the device crosses the geofence, the app can send a notification that takes users to an activity that allows them to get their boarding pass.
The Geofencing API intelligently uses the device sensors to accurately detect the location of the device in a battery-efficient way.
You can create a list of geofence objects by setting the latitude, longitude, radius, duration, and transition types of each geofence. The transition types indicate the events that trigger the geofence, such as when users enter or exit a geofence.
Once you have a list of geofences, you can add it to a geofencing request. When it's time to start monitoring the geofences, add the request to a geofencing client along with a
PendingIntent object, which tells the API how to deliver the geofencing events to your app.
The Geofencing API delivers the events to an
IntentService in your app, which removes the need to have a service running in the background for geofencing purposes. The service is only invoked when there's relevant information.
Your service receives the geofencing event from the
Intent, including the list of geofences triggered. You can specify your own logic to decide what actions to take.