Create and Monitor Geofencing in Android
Posted By : Prince Bhardwaj | 31-Dec-2018
Introduction
Geofencing consolidates awareness of the client's current location with a familiarity with the client's nearness to locations that might be of interest for the client. To highlight an area of interest, you indicate its latitude and longitude. To modify the nearness for the location, you include a radius. The latitude, longitude, and radius characterize a geofence, making a circular area, or fence, around the area of interest.
We can have multiple dynamic geofences, with a limit of 100 for every device user overall applications. For each geofence, we can ask Location Services to send you to enter and exit occasions, or we can also indicate a time period inside the geofence territory to pause or dwell, before triggering an event. We can limit the duration of any geofence by setting an expiration duration in milliseconds. After the geofence terminates, Location Services consequently removes it.
Now, we will discuss how to add geofence events in an android application and listen to them when the user enters or stay at a location(dwell).
1. Setup Geofence
To set geofence in our app, we first need to have ACCESS_FINE_LOCATION
permission granted. To request this permission we need to ask this permission in our manifest file as :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> |
For listening to geofence events, we have used a receiver where we receive geofence triggers. For this, add an element referring to the receiver as -
<application |
2. Create and Add Geofences
We first need to connect to Location Services API. For this, we have to create an instance of Geofencing client as -
GeofencingClient geofencingClient = LocationServices.getGeofencingClient(mContext);
Now we need to add geofences to the geofence list. To create a geofence, we have to use Geofence.Builder() as
private Geofence getGeofence(double lat, double lang, String key) { |
In this example, we have used two transitions-
We can also use
The above method will return a geofence object to be added in geofence list and to be used to create geofence request as -
private GeofencingRequest getGeofencingRequest(ArrayList<Geofence> geofences) { |
Determining INITIAL_TRIGGER_ENTER indicates Location services that GEOFENCE_TRANSITION_ENTER transition should be triggered if the device is already inside the geofence.
In most of the cases, it might be desirable overuse rather INITIAL_TRIGGER_DWELL, which triggers events just when the client stops for a specified duration inside a geofence.
To listen to geofence events, we should add a pending intent to our geofence client where we receive all the triggered events.
private PendingIntent getGeofencePendingIntent() { |
To add geofences, we have to use GeofencingClient.addGeofences() method. Give the GeofencingRequest object, and the PendingIntent. The accompanying piece shows handling the results:
geofencingClient.addGeofences(getGeofencingRequest(geofences), |
3. Listen to Geofence Transitions
At the point when Location Services detects that the user has entered or dwells in a geofence area, it conveys the Intent contained in the PendingIntent you incorporated into the request to include geofences. This Intent is received by a service like GeofenceTransitionsIntentService, which gets the geofencing event from the intent, decides the type of Geofence transition(s), and figures out which of the defined geofences was triggered.
In this example, we have used JobIntentService keeping in mind the Background Services Limits On Android 8.0 (API level 26) and higher.
public class LocationAlertIntentService extends JobIntentService { |
4. Stop/Remove Geofence Monitoring
It is best practice to remove geofence monitoring when it is no longer needed. We can remove geofence in the main activity used to add geofences. We can remove a geofence either by pending
Intent or by requests. Here are the two methods which are used to remove geofences by pending intent -
public void removeLocationAlerts(){ |
and by requestIds-
public void removeLocationAlerts(List<String> requestIds) { |
Best Practices for Geofencing
- 1. Use Dwell Transition to reduce alert spam.
- 2. Re-register geofences only when required.
The application must re-register geofences if they're required after the accompanying events since the system can't recover the geofences in the following cases:
The device is rebooted. The application ought to tune in for the device's boot finished activity, and after that re-register the geofences required.
The application is uninstalled and re-installed.
The application's data is cleared.
Google Play services data is cleared.
The application has received a GEOFENCE_NOT_AVAILABLE alarm. This ordinarily occurs after NLP (Android's Network Location Provider) is disabled.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Bhardwaj
Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.