Creating and Monitoring Geofences

What is Geofence?

Geofencing is a location-based service that sends a notification to smartphone users who enter a defined geographic area. Some companies send promotions to customers’ smartphones when they enter a store, mall or neighborhood.

geofence viewApplications of Geofence

Marketers can use geofencing by geofencing a retail store and send a coupon to a customer with a mobile app crosses the boundary.

Geofencing is used by the human resource department to monitor employees working in special locations

Geofencing, used with child location services, can notify parents if a child leaves a designated area.

1.Overview

To use geofencing, start by defining the geofences you want to monitor.Each Geofence object contains the following information:

  1. Latitude, longitude, and radius
  2. Expiration time
  3. Transition type
  4. Geofence ID

2.Set up for Geofence Monitoring

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

3.Set Up Google Play Service

Access the location provider, the project must include Google Play services. 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.

4.Connect to Google Play Services

To connect to the API, you need to create an instance of the Google Play services API client. In your activity’s onCreate() method, create an instance of Google API Client, using the 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.

5.Request Location Updates

Before requesting location updates, your app must connect to location services and make 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.

6.Create geofence objects

To create geofence object we 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

GEOFENCE_TRANSITION_ENTER:Transition triggers when a device enters a geofence GEOFENCE_TRANSITION_EXIT:Transition triggers when a device exits a geofence. INITIAL_TRIGGER_ENTER: Tells Location services that  should be triggered if the the device is already inside the geofence.                                                       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.

7.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:

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?

The app must re-register geofences if they’re still needed after the following events, since the system cannot recover the geofences in the following cases:

  • 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.

 

Registered geofences are kept in the com.google.process.location process owned by the com.google.android.gms package. The app doesn’t need to do anything to handle the following events, because the system restores geofences after these events:

  • Google Play services is upgraded.
  • Google Play services is killed and restarted by the system due resource restriction.
  • The location process crashes.

Download this project from GitHub.

Related Posts

Android Bluetooth Low Energy Client

Android MQTT Client

 

 

One Reply to “Creating and Monitoring Geofences”

  1. I’ve tried this tutorial, but I have a problem. How to make Button enable when entering geofence and disable when outta geofence on activity ?…

Leave a Reply

Your email address will not be published. Required fields are marked *