Geofencing is a location-based service, that sends a notification to a smartphone who enter a defined geographic area.
For example, human resource department to monitor employees working in special locations or child location services can notify parents if a child leaves a designated area.

1.Set up for Permission

To use geofencing, your app must request ACCESS_FINE_LOCATION.To request this permission, add the following element as a child element of the <manifest>element in your app manifest

2.Google Play Service

Google Play services are used for access the location provider.Download and install the Google Play services component via the SDK Manager and add the library to your project. For details, see the guide to Setting Up Google Play Services

3.Connect to Google Play Services

Create an instance of Google API Client in activity’s onCreate() method. Use GoogleApiClient.Builderclass to add the LocationServices API, as the following code snippet shows.

Android 6.0+ (API level 23+), users grant permissions to apps while the app is running, not when they install the app.

Here is an example activity that implements the callback interfaces that adds  to the Google API Client

To connect, call connect() from the activity’s onStart() method. To disconnect, call disconnect() from the activity’s onStop() method.

4.Request Location Updates

Your app must connect to location services and make then a location request.Once a location request is in place you can start the regular updates by calling requestLocationUpdates().

Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready.

Stop Location Updates

whether you want to stop the location updates when the activity is no longer in focus.

5.Create geofence objects

To create geofence object you need Latitude, Longitude, and Radius, Expiration time, Transition type, Geofence ID

Your app needs to create and add geofences using the location API’s builder class for creating Geofence objects, and the convenience class for adding them.

Specify geofences and initial triggers

The following snippet uses the GeofencingRequest class and its nested GeofencingRequestBuilder class to specify the geofences to monitor and to set how related geofence events are triggered

Geofence triggers

  1. GEOFENCE_TRANSITION_ENTER: Triggers when a device enters a geofence
  2. GEOFENCE_TRANSITION_EXIT: Triggers when a device exits a geofence.
  3. INITIAL_TRIGGER_ENTER: Tells Location services that should be triggered if the device is already inside the geofence.
  4. INITIAL_TRIGGER_DWELL: Triggers events only when the user stops for a defined duration within a geofence. This approach can help reduce “alert spam” resulting from large numbers notifications.

6.Handle Geofence Transitions

When Location Services detects that the user has entered or exited a geofence, it sends out the Intent contained in the PendingIntent you included in the request to add geofences. This Intent is received by a Service which obtains the geofencing event from the intent, determines the type of Geofence transition(s), and determines which of the defined geofences was triggered. It then sends a notification as the output.

IntentService to listen for geofence transitions, add an element specifying the service name. This element must be a child of the <application> element:

The following snippet shows how to define an IntentService that log when a geofence transition occurs. You can define notification. When the user clicks the notification, the app’s main activity appears

Define an Intent for geofence transitions

The Intent sent from Location Services can trigger various actions in your app.IntentService is a good way to handle the intent. An IntentService can post a notification, do long-running background work, send intents to other services, or send a broadcast intent. The following snippet shows how to define a PendingIntent that starts an IntentService:

Add geofences

To add geofences, use the GeofencingApi.addGeofences() method. Provide the Google API client, the GeofencingRequest object, and the PendingIntent. The following snippet, which processes the results in onResult(),  implements ResultCallback:

7.Stop Geofence Monitoring

The following snippet removes geofences by PendingIntent, stopping all further notification when the device enters or exits previously added geofences:

When geofences required  Re-register?

Registered geofences are kept in the process owned by the package.

  • The device is rebooted.
  • The app is uninstalled and re-installed.
  • The app’s data is cleared.
  • Google Play services data is cleared.
  • The app has received a GEOFENCE_NOT_AVAILABLE alert. This typically happens after NLP (Android’s Network Location Provider) is disabled.



Download this project from GitHub.


1 thought on “Creating and Monitoring Geofences”

Leave a Reply