How to create a geofence app in android.

A step by step guide in creating a Geofence application in android and making use of BroadcastReciever to start a Service when that location is reached.

What is Geofence?

A geofence is a virtual perimeter for a real-world geographic area. A geo-fence could be dynamically generated — as in a radius around a point location, or a geo-fence can be a predefined set of boundaries.

Want to read this story later? Save it in Journal.

How do we make use of this in Android?

  1. Keep track of movements in and out of a geofence.
  2. Provide location-based notification.
  3. Provide real-time guidance via the application.

the use cases are endless, Enough mumbo jumbo let’s get down to business.

Prerequisite.

  1. Knowledge of Java and Android studio.
  2. Understanding of the working principles of services and Broadcasts in android.
  3. Good understanding of the activity life cycle.

Let’s code…

Step 1: Create a new empty project and name it anything u want preferably Geofence app.

Step 2: Add the necessary dependency.

in your project level Gradle file add this

in your app-level Gradle file add these

Step 3: Now we will open up the main activity and create a geofence area for a set of latitude and longitude with some radius.

this is how you can add a set of geofence in an ArrayList in case you have multiple Geofences to tend to.

here we have some important points they are.

unique id for each Geofence : .setRequestId(“unique id”)

latitude and longitude in Double format : Double.parseDouble(locationModel.getLat()) Double.parseDouble(locationModel.getLon())

radius in float format : (float) radius)

Geofencetype: .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)

Geofence type can be enter, exit, etc. I gave enter here so every time a device enters a geofence it will be activated.

Now we build a Geofence, let’s activate it .

Step 4: Adding a Geofence to

These methods as their name suggests will enable you to start or end a geofence action. You can call them whenever necessary as in a button click or in a lifecycle method.

Step 5: Now we will create a method to listen to geofence and take necessary actions when it is fired.

In these methods, we are creating a pendingIntent and waiting for a response from Geofence and once we get the confirmation we fire a Broadcast. The name if my BrodcastReceiver is MyBroadCastReciever. You can name your anything.

Step 6: Creating a BroadcastReceiver.

Here you need to create a new BroadcastReceiver class and write the following methods in it.

This here will help you send a notification and start a Service at the same time. If the device targeted is above Android O it will start a foreground service.

Step 7: Create a class called App and extend from class Application and paste the following code in it.

Step 8: Create a Service class and name it anything you want, but appropriate to the function of it. Here I am creating a class called LocationService and show you how you can perform a task on the service.

Here we start a service on the method onStartCommand(). Here we are setting up a notification badge to perform a foreground service and at the same time, we are starting a background task using AsyncTask to perform a download operation.

Step 9: Add the necessary fields in the Manifest file.

All the necessary permissions must be granted and also these necessary fields including maps API key needs to be added.

And that's it, you have successfully created a Geofence application. If u have any doubts feel free to share in the comment section.

Thank you, hope your learned something new.

📝 Save this story in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.