Geofencing in Android, A Simple Example.

By | June 20, 2016

What is Geofencing?

Geo-fencing is a feature in a software program that uses the global positioning system (GPS) or radio frequency identification (RFID) to define geographical boundaries. A geofence is a virtual barrier.

Today we will discuss how we can implement Geofencing in Android with a single location.

First You need to provide the permissions in the AndroidManifest.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

Geofencing needs Google Services API.
Open your build.gradle and add the dependency.

    compile ''

Here we are going to set a Fence of radius around our current location or the Last Known location in our Device.

Implement Geofencing.

First we have to check whether this device has Google Play Services Installed.

int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resp == ConnectionResult.SUCCESS) {
	mGoogleApiClient = new GoogleApiClient.Builder(this)
} else {
	Log.e(TAG, "Your Device doesn't support Google Play Services.");

For getting the last known location, we could use GoogleServices API itself.

	// Create the LocationRequest object
	mLocationRequest = LocationRequest.create()
		.setInterval(10 * 1000)        // 10 seconds, in milliseconds
		.setFastestInterval(1 * 1000); // 1 second, in milliseconds

Now implement “LocationListener” of GoogleServices API with the below methods.

Get the Location

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (location == null) {

	LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,    mLocationRequest, this);

} else {

	currentLatitude = location.getLatitude();
	currentLongitude = location.getLongitude();


Location Listeners

public void onLocationChanged(Location location) {

	currentLatitude = location.getLatitude();
	currentLongitude = location.getLongitude();


Now we will add both Success and Fail Listeners.

private GoogleApiClient.ConnectionCallbacks connectionAddListener =
	new GoogleApiClient.ConnectionCallbacks() {
		public void onConnected(Bundle bundle) {

		public void onConnectionSuspended(int i) {

private GoogleApiClient.OnConnectionFailedListener connectionFailedListener =
	new GoogleApiClient.OnConnectionFailedListener() {
		public void onConnectionFailed(ConnectionResult connectionResult) {


Adding Geofencing

 * Create a Geofence list by adding all fences you want to track
public void createGeofences(double latitude, double longitude) {

	String id = UUID.randomUUID().toString();
	Geofence fence = new Geofence.Builder()
			.setCircularRegion(latitude, longitude, 200) // Try changing your radius


Create Geofencing Request and trigger

private GeofencingRequest getGeofencingRequest() {

	GeofencingRequest.Builder builder = new GeofencingRequest.Builder();


But wait…we need a class to Listen to…right…
So we will create an Intent Service and add the Pending Intent to it.

Add PendingIntent

private PendingIntent getGeofencePendingIntent() {

	// Reuse the PendingIntent if we already have it.
	if (mGeofencePendingIntent != null) {
		return mGeofencePendingIntent;
	Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
	// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
	// calling addGeofences() and removeGeofences().
	return PendingIntent.getService(this, 0, intent, PendingIntent.


Create a class “GeofenceTransitionsIntentService” that extends IntentService.

package com.coderzheaven.geofencedemo;

import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class GeofenceTransitionsIntentService extends IntentService {

    private static final String TAG = "GeofenceTransitions";

    public GeofenceTransitionsIntentService() {

    protected void onHandleIntent(Intent intent) {

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            Log.e(TAG, "Goefencing Error " + geofencingEvent.getErrorCode());

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER){
            showNotification("Entered", "Entered the Location");
        else if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
            showNotification("Exited", "Exited the Location");
        } else {
            showNotification("Error", "Error");

    public void showNotification(String text, String bigText) {

        // 1. Create a NotificationManager
        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        // 2. Create a PendingIntent for AllGeofencesActivity
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

          // 3. Create and send a notification
        Notification notification = new NotificationCompat.Builder(this)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
        notificationManager.notify(0, notification);

Download the Complete Android Studio Source Code from here.