{"id":6994,"date":"2020-09-02T13:16:37","date_gmt":"2020-09-02T13:16:37","guid":{"rendered":"https:\/\/www.rlogical.com\/?p=6994"},"modified":"2025-03-31T18:47:21","modified_gmt":"2025-03-31T13:17:21","slug":"how-to-integrate-geo-fencing-in-an-android-app","status":"publish","type":"post","link":"https:\/\/www.rlogical.com\/blog\/how-to-integrate-geofencing-in-an-android-app\/","title":{"rendered":"How to integrate Geo-fencing in an Android App?"},"content":{"rendered":"<h2><span style=\"color: #003366;\"><strong>What is Geo fencing?<\/strong><\/span><\/h2>\n<p>Geofencing is a service that triggers an action when a device enters or exits from the specified predefined location or geographical area for a given radius. This is a location-based service in which a creator of an app or other platform\/software uses <strong>GPS, RFID, Wi-Fi<\/strong> or cellular data to send\/trigger messages like <strong>SMS, Email or In-App\/App-based notifications (pre-programmed action)<\/strong> when mobile devices that enter, exit center or remain parked in the geographical location.<\/p>\n<p>Android allows you to create a geo-fence by specifying the radius and lat-long (short for latitude and longitude) of the center of the real geographical region that you wish to monitor.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-7020 size-full aligncenter\" src=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2023\/03\/Geofencing-in-Android.webp\" alt=\"Geofencing in Android\" \/><\/p>\n<p>There are smart companies that send product offers or specific promotions to consumers\u2019 smartphones when they trigger a search in a particular geographic location, enter a mall, neighborhood, or store.<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>How Does Geo fencing Work?<\/strong><\/span><\/h2>\n<p>So far, we\u2019ve covered a lot about Geo fencing and what it can do for your business. Do you know how it works?<\/p>\n<p>It\u2019s simple though. Geo fencing helps you to keep in control of your business by notifying you when a potential consumer is passing by your store, by a competitor\u2019s, or entering into a predefined area.<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>Advantages of Geo fencing in App<\/strong><\/span><\/h2>\n<ul style=\"list-style-type: disc;\">\n<li>It helps in targeting customers in shops or retail outlets with appropriate ads as well as direct them to desired sections in order to increase sales.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc;\">\n<li>It helps in influencing purchase decisions.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc;\">\n<li>It does not require additional hardware to implement it. Mobile apps are available for android and iOS operating systems.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc;\">\n<li>It is very easy to implement and use with the help of google maps.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc;\">\n<li>It is very cheaper due to less costly mobile phones and availability of free apps.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>Disadvantages of Geo fencing in App\u00a0<\/strong><\/span><\/h2>\n<ul style=\"list-style-type: disc;\">\n<li>The size and accuracy of the Geo fences depends on the location and positioning technologies used. The Geo fencing positioning technologies such as GPS, cellular and Wi-Fi deliver accuracy from 20 meters to 50 meters. Smaller Geo fencing use Bluetooth positioning technologies such as iBeacons and Eddystone beacons. These technologies deliver accuracy of about 2 meters.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc;\">\n<li>GPS positioning technique can also be used for Geo fencing but due to higher power consumption it is not recommended for battery hungry devices.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #003366;\"><strong>Obtaining a Google Maps API Key<\/strong><\/span><\/h3>\n<p>Because this app uses Google Maps, you\u2019ll need to obtain an API key.<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>Open\u00a0<strong>Google Cloud Platform <\/strong>and\u00a0<a href=\"https:\/\/console.cloud.google.com\/projectcreate\">create a new project<\/a>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6996 size-full\" src=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2020\/09\/Google-cloud-platform.png\" alt=\"Geo-fencing in an Android App\" width=\"850\" height=\"427\" srcset=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2020\/09\/Google-cloud-platform.png 850w, https:\/\/www.rlogical.com\/wp-content\/uploads\/2020\/09\/Google-cloud-platform-300x151.png 300w, https:\/\/www.rlogical.com\/wp-content\/uploads\/2020\/09\/Google-cloud-platform-768x386.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>Select\u00a0<em><strong>APIs &amp; Services <\/strong><\/em><em><strong>\u25b8<\/strong><\/em><em><strong> Maps SDK for Android <\/strong><\/em><em><strong>\u25b8<\/strong><\/em><em><strong> Enable<\/strong><\/em>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6997 size-full\" src=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2023\/03\/Maps-SDK-for-Android.webp\" alt=\"Maps SDK for Android\" width=\"532\" height=\"274\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>Click on\u00a0<em><strong>Credentials<\/strong><\/em>,\u00a0<em><strong>Credentials in the API Manager<\/strong><\/em>,\u00a0<em><strong>CREATE CREDENTIALS <\/strong><\/em>and then choose\u00a0<em><strong>API key<\/strong><\/em>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6998 size-full\" src=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2023\/03\/Credentials-in-the-API-Manager.webp\" alt=\"Credentials in the API Manager\" width=\"463\" height=\"289\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>Copy your API key value. In your project, replace\u00a0<strong>YOUR_KEY_HERE<\/strong> with the copied value.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>In your project level Gradle file add this<\/strong><\/span><\/h2>\n<pre><span style=\"color: #333399;\">classpath 'com.google.gms:google-services:4.3.3'<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>In your app-level Gradle file add these<\/strong><\/span><\/h2>\n<p>Using geofences requires the\u00a0play-services-location\u00a0library added to your project. To do that, open the\u00a0build.gradle\u00a0file for the\u00a0app\u00a0module and add the following dependency:<\/p>\n<pre><span style=\"color: #333399;\">apply plugin: 'com.google.gms.google-services'\u00a0<\/span>\r\n\r\n<span style=\"color: #333399;\">dependencies <strong>{<\/strong>\r\nimplementation <strong>'com.google.android.gms:play-services-location:17.0.0'\r\n<\/strong>implementation <strong>'com.google.android.gms:play-services-maps:17.0.0'\r\n<\/strong><strong>}<\/strong><\/span><strong><span style=\"color: #333399;\">\u00a0<\/span>\r\n\r\n<\/strong><\/pre>\n<h2><span style=\"color: #003366;\"><strong>In your Activity Class<\/strong><\/span><\/h2>\n<p>You have to implements <span style=\"color: #333399;\"><strong>OnMapReadyCallback<\/strong>, <strong>GoogleApiClient.ConnectionCallbacks<\/strong><\/span> and <span style=\"color: #333399;\"><strong>GoogleApiClient.OnConnectionFailedListener<\/strong> <\/span>in activity class<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>Define variable in activity class:<\/strong><\/span><\/h2>\n<pre><span style=\"color: #333399;\"><strong>private static final int <\/strong><strong><em>REQUEST_LOCATION_PERMISSION_CODE <\/em><\/strong>= 101;<\/span>\r\n<span style=\"color: #333399;\">\r\n<strong>public static final <\/strong>String <strong><em>GEOFENCE_ID <\/em><\/strong>= <strong>\"RLOGICAL\"<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>public static final float <\/strong><strong><em>GEOFENCE_RADIUS_IN_METERS <\/em><\/strong>= 100;<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>public <\/strong>HashMap&lt;String, LatLng&gt; <strong>LOCATION_LIST <\/strong>= <strong>new <\/strong>HashMap&lt;String, LatLng&gt;();<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>private <\/strong>GoogleMap <strong>googleMap<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>GeofencingRequest <strong>geofencingRequest<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>GoogleApiClient <strong>googleApiClient<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private boolean <\/strong><strong>isMonitoring <\/strong>= <strong>false<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>MarkerOptions <strong>markerOptions<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>Marker <strong>currentLocationMarker<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>PendingIntent <strong>pendingIntent<\/strong>;\r\n\r\n<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>Add Location for where you have to create geofence.<\/strong><\/span><\/h2>\n<pre><span style=\"color: #333399;\"><strong>LOCATION_LIST<\/strong>.put(<strong><em>GEOFENCE_ID<\/em><\/strong>, <strong>new <\/strong>LatLng(23.029840, 72.557776));\r\n\r\n<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>Connect with Google API<\/strong><\/span><\/h2>\n<p>GoogleApiClient is used with a variety of static methods. Some of these methods require that GoogleApiClient be connected, some will queue up calls before GoogleApiClient is connected; check the specific API documentation to determine whether you need to be connected.<\/p>\n<p><span style=\"color: #333399;\"><strong>googleApiClient <\/strong>= <strong>new <\/strong>GoogleApiClient.Builder(<strong>this<\/strong>) .addApi(LocationServices.<strong><em>API<\/em><\/strong>).addConnectionCallbacks(<strong>this<\/strong>).addOnConnectionFailedListener(<strong>this<\/strong>).build();<\/span><\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<h3><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"text-decoration: underline;\">Secrets You Didn\u2019t Know<\/span><\/strong>: <a href=\"https:\/\/www.rlogical.com\/blog\/how-to-hire-android-app-developers\/\" target=\"_blank\" rel=\"noopener\">How to Hire Android App Developers<\/a><\/h3>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>Check Location Permission<\/strong><\/span><\/h2>\n<p>The first step in requesting geofence monitoring is to request the necessary permissions.<\/p>\n<p>If your app needs to access the user&#8217;s location, you must request permission by adding the relevant Android\u00a0<strong>location permission<\/strong>\u00a0to your app.<\/p>\n<pre><span style=\"color: #333399;\"><strong>if <\/strong>(ActivityCompat.<em>checkSelfPermission<\/em>(<strong>this<\/strong>, Manifest.permission.<strong><em>ACCESS_FINE_LOCATION<\/em><\/strong>) != \r\nPackageManager.<strong><em>PERMISSION_GRANTED <\/em><\/strong>&amp;&amp; ActivityCompat.<em>checkSelfPermission<\/em>(<strong>this<\/strong>, \r\nManifest.permission.<strong><em>ACCESS_COARSE_LOCATION<\/em><\/strong>) != PackageManager.<strong><em>PERMISSION_GRANTED<\/em><\/strong>) {<\/span>\r\n<span style=\"color: #333399;\">ActivityCompat.<em>requestPermissions<\/em>(<strong>this<\/strong>, <strong>new \r\n<\/strong>String[]{Manifest.permission.<strong><em>ACCESS_FINE_LOCATION<\/em><\/strong>, \u00a0\r\nManifest.permission.<strong><em>ACCESS_COARSE_LOCATION<\/em><\/strong>}, <strong><em>REQUEST_LOCATION_PERMISSION_CODE<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}\r\n\r\n<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>Start Location tracking<\/strong><\/span><\/h2>\n<p>Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/location\/LocationManager#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)\"><strong>LocationManager#requestLocationUpdates(String, long, float, LocationListener)<\/strong><\/a><strong>\u00a0<\/strong>method.<\/p>\n<pre><span style=\"color: #333399;\"><strong>private void <\/strong>startLocationMonitor() {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Start Location monitor\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">LocationRequest locationRequest = LocationRequest.<em>create<\/em>()<\/span>\r\n<span style=\"color: #333399;\">.setInterval(2000)<\/span>\r\n<span style=\"color: #333399;\">.setFastestInterval(1000)<\/span>\r\n<span style=\"color: #333399;\">.setPriority(LocationRequest.<strong><em>PRIORITY_HIGH_ACCURACY<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>try <\/strong>{<\/span>\r\n<span style=\"color: #333399;\">LocationServices.<strong><em>FusedLocationApi<\/em><\/strong>.requestLocationUpdates(<strong>googleApiClient<\/strong>, locationRequest, <strong>new <\/strong>LocationListener() {<\/span>\r\n<span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onLocationChanged(Location location) {<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(<strong>currentLocationMarker <\/strong>!= <strong>null<\/strong>) {<\/span>\r\n<span style=\"color: #333399;\"><strong>currentLocationMarker<\/strong>.remove();<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\"><strong>markerOptions <\/strong>= <strong>new <\/strong>MarkerOptions();<\/span>\r\n<span style=\"color: #333399;\"><strong>markerOptions<\/strong>.position(<strong>new <\/strong>LatLng(location.getLatitude(), location.getLongitude()));<\/span>\r\n<span style=\"color: #333399;\"><strong>markerOptions<\/strong>.title(<strong>\"Current Location\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>currentLocationMarker <\/strong>= <strong>googleMap<\/strong>.addMarker(<strong>markerOptions<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Location Change Lat Lng \" <\/strong>+ location.getLatitude() + <strong>\" \" <\/strong>+ location.getLongitude());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">});<\/span>\r\n<span style=\"color: #333399;\">} <strong>catch <\/strong>(SecurityException e) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, e.getMessage());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">}\r\n<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>Start Monitoring Geo Fence tracking<\/strong><\/span><\/h2>\n<p>Geofencing combines awareness of the user&#8217;s current location with awareness of the user&#8217;s proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest.<\/p>\n<p>To add geofences, use the\u00a0<a href=\"https:\/\/developers.google.com\/android\/reference\/com\/google\/android\/gms\/location\/GeofencingClient.html#addGeofences(com.google.android.gms.location.GeofencingRequest,%20android.app.PendingIntent)\"><strong>GeofencingClient.addGeofences()<\/strong><\/a>\u00a0method. Provide the\u00a0<a href=\"https:\/\/developers.google.com\/android\/reference\/com\/google\/android\/gms\/location\/GeofencingRequest\"><strong>GeofencingRequest<\/strong><\/a>\u00a0object, and the\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/app\/PendingIntent\"><strong>PendingIntent<\/strong><\/a><\/p>\n<pre><span style=\"color: #333399;\"><strong>private void <\/strong>startGeofencing() {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Start geofencing monitoring call\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>pendingIntent <\/strong>= getGeofencePendingIntent();<\/span>\r\n<span style=\"color: #333399;\"><strong>geofencingRequest <\/strong>= <strong>new <\/strong>GeofencingRequest.Builder()<\/span>\r\n<span style=\"color: #333399;\">.setInitialTrigger(Geofence.<strong><em>GEOFENCE_TRANSITION_ENTER<\/em><\/strong>)<\/span>\r\n<span style=\"color: #333399;\">.addGeofence(getGeofence())<\/span>\r\n<span style=\"color: #333399;\">.build();<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(!<strong>googleApiClient<\/strong>.isConnected()) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Google API client not connected\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">} <strong>else <\/strong>{<\/span>\r\n<span style=\"color: #333399;\"><strong>try <\/strong>{<\/span>\r\n<span style=\"color: #333399;\">LocationServices.<strong><em>GeofencingApi<\/em><\/strong>.addGeofences(<strong>googleApiClient<\/strong>, <strong>geofencingRequest<\/strong>, <strong>pendingIntent<\/strong>).setResultCallback(<strong>new <\/strong>ResultCallback&lt;Status&gt;() {<\/span>\r\n<span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onResult(@NonNull Status status) {<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(status.isSuccess()) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Successfully Geofencing Connected\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">} <strong>else <\/strong>{<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Failed to add Geofencing \" <\/strong>+ status.getStatus());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">});<\/span>\r\n<span style=\"color: #333399;\">} <strong>catch <\/strong>(SecurityException e) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, e.getMessage());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\"><strong>isMonitoring <\/strong>= <strong>true<\/strong>;<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">@NonNull<\/span>\r\n<span style=\"color: #333399;\"><strong>private <\/strong>Geofence getGeofence() {<\/span>\r\n<span style=\"color: #333399;\">LatLng latLng = <strong>LOCATION_LIST<\/strong>.get(<strong><em>GEOFENCE_ID<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>return new <\/strong>Geofence.Builder().setRequestId(<strong><em>GEOFENCE_ID<\/em><\/strong>).setExpirationDuration(Geofence.<strong><em>NEVER_EXPIRE<\/em><\/strong>)<\/span>\r\n<span style=\"color: #333399;\">.setCircularRegion(latLng.<strong>latitude<\/strong>, latLng.<strong>longitude<\/strong>, <strong><em>GEOFENCE_RADIUS_IN_METERS<\/em><\/strong>).setNotificationResponsiveness(1000)<\/span>\r\n<span style=\"color: #333399;\">.setTransitionTypes(Geofence.<strong><em>GEOFENCE_TRANSITION_ENTER <\/em><\/strong>| Geofence.<strong><em>GEOFENCE_TRANSITION_EXIT<\/em><\/strong>).build();<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>Start Intent service in background<\/strong><\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>private <\/strong>PendingIntent getGeofencePendingIntent() {<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(<strong>pendingIntent <\/strong>!= <strong>null<\/strong>) {<\/span>\r\n<span style=\"color: #333399;\"><strong>return <\/strong><strong>pendingIntent<\/strong>;<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">Intent intent = <strong>new <\/strong>Intent(<strong>this<\/strong>, GeofenceService.<strong>class<\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>return <\/strong>PendingIntent.<em>getService<\/em>(<strong>this<\/strong>, 0, intent, PendingIntent.<\/span>\r\n<span style=\"color: #333399;\"><strong><em>FLAG_UPDATE_CURRENT<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">@<span style=\"color: #666699;\">Override<\/span><\/span>\r\n<span style=\"color: #333399;\"><strong>protected void <\/strong>onResume() {<\/span>\r\n<span style=\"color: #333399;\"><strong>super<\/strong>.onResume();<\/span>\r\n<span style=\"color: #333399;\"><strong>int <\/strong>response = GoogleApiAvailability.<em>getInstance<\/em>().isGooglePlayServicesAvailable(MainActivity.<strong>this<\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(response != ConnectionResult.<strong><em>SUCCESS<\/em><\/strong>) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Google Play Service Not Available\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">GoogleApiAvailability.<em>getInstance<\/em>().getErrorDialog(MainActivity.<strong>this<\/strong>, response, 1).show();<\/span>\r\n<span style=\"color: #333399;\">} <strong>else <\/strong>{<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Google play service available\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366;\"><strong>Draw geofence on Google Map<\/strong><\/span><\/h2>\n<p>If Google Play services is not installed on the device, the user will be prompted to install it, and the\u00a0<a href=\"https:\/\/developers.google.com\/android\/reference\/com\/google\/android\/gms\/maps\/OnMapReadyCallback#onMapReady(com.google.android.gms.maps.GoogleMap)\">onMapReady(GoogleMap)<\/a>\u00a0method will only be triggered when the user has installed it and returned to the app.<\/p>\n<pre><span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onMapReady(GoogleMap googleMap) {<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(ActivityCompat.<em>checkSelfPermission<\/em>(<strong>this<\/strong>, Manifest.permission.<strong><em>ACCESS_FINE_LOCATION<\/em><\/strong>) != PackageManager.<strong><em>PERMISSION_GRANTED <\/em><\/strong>&amp;&amp; ActivityCompat.<em>checkSelfPermission<\/em>(<strong>this<\/strong>, Manifest.permission.<strong><em>ACCESS_COARSE_LOCATION<\/em><\/strong>) != PackageManager.<strong><em>PERMISSION_GRANTED<\/em><\/strong>) {<\/span>\r\n<span style=\"color: #333399;\"><strong>return<\/strong>;<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>this<\/strong>.<strong>googleMap <\/strong>= googleMap;<\/span>\r\n<span style=\"color: #333399;\">LatLng latLng = <strong>LOCATION_LIST<\/strong>.get(<strong><em>GEOFENCE_ID<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">googleMap.addMarker(<strong>new <\/strong>MarkerOptions().position(latLng).title(<strong>\"RLogical\"<\/strong>));<\/span>\r\n<span style=\"color: #333399;\">googleMap.moveCamera(CameraUpdateFactory.<em>newLatLngZoom<\/em>(latLng, 17f));<\/span>\r\n<span style=\"color: #333399;\">googleMap.setMyLocationEnabled(<strong>true<\/strong>);<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>\/\/ Draw Geofence on Map\r\n<\/em>Circle circle = googleMap.addCircle(<strong>new <\/strong>CircleOptions().center(<strong>new <\/strong>LatLng(latLng.<strong>latitude<\/strong>, latLng.<strong>longitude<\/strong>))<\/span>\r\n<span style=\"color: #333399;\">.radius(<strong><em>GEOFENCE_RADIUS_IN_METERS<\/em><\/strong>).strokeColor(Color.<strong><em>RED<\/em><\/strong>).strokeWidth(4f));<\/span>\r\n<span style=\"color: #333399;\">}\u00a0<strong>Connect with Google and Start Monitoring<\/strong>\u00a0@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onConnected(@Nullable Bundle bundle) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Google Api Client Connected\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\"><strong>isMonitoring <\/strong>= <strong>true<\/strong>;<\/span>\r\n<span style=\"color: #333399;\">startGeofencing();<\/span>\r\n<span style=\"color: #333399;\">startLocationMonitor();<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onConnectionSuspended(<strong>int <\/strong>i) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Google Connection Suspended\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>public void <\/strong>onConnectionFailed(@NonNull ConnectionResult connectionResult) {<\/span>\r\n<span style=\"color: #333399;\"><strong>isMonitoring <\/strong>= <strong>false<\/strong>;<\/span>\r\n<span style=\"color: #333399;\">Log.<em>e<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"Connection Failed:\" <\/strong>+ connectionResult.getErrorMessage());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n\r\n<\/pre>\n<h2><span style=\"text-decoration: underline;\"><span style=\"color: #003366; text-decoration: underline;\"><strong>Service Class:<\/strong><\/span><\/span><\/h2>\n<p><span style=\"color: #003366;\"><strong>Define an Intent Service for geofence transitions<\/strong><\/span><\/p>\n<p>An\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/content\/Intent\">Intent<\/a> sent from Location Services can trigger various actions in your app, but you should not have it start an activity or fragment, because components should only become visible in response to a user action. In many cases, an Intent Service is a good way to handle a geofence transition. An Intent Service gets updates when an event occurs, such as a transition into or out of a geofence, and can start long-running background work.<\/p>\n<p>When Location Services detects that the user has entered or exited a geofence, it sends out the\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/content\/Intent\"><strong>Intent<\/strong><\/a>\u00a0contained in the\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/app\/PendingIntent\"><strong>PendingIntent<\/strong><\/a>\u00a0you included in the request to add geofences. A intent service like\u00a0<strong>Geofence Service<\/strong>\u00a0notices that the\u00a0<a href=\"https:\/\/developer.android.com\/reference\/android\/content\/Intent\">Intent<\/a>\u00a0was invoked and can then obtain the geofencing event from the intent, determine the type of Geofence transition(s), and determine which of the defined geofences was triggered. The Intent service can direct an app to start performing background work or, if desired, send a notification as output.<\/p>\n<pre><span style=\"color: #333399;\"><strong>public class <\/strong>GeofenceService <strong>extends <\/strong>IntentService {<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>private static final <\/strong>String <strong><em>TAG <\/em><\/strong>= <strong>\"GeoIntentService\"<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>public <\/strong>GeofenceService() {<\/span>\r\n<span style=\"color: #333399;\"><strong>super<\/strong>(<strong><em>TAG<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">@Override<\/span>\r\n<span style=\"color: #333399;\"><strong>protected void <\/strong>onHandleIntent(@Nullable Intent intent) {<\/span>\r\n<span style=\"color: #333399;\">GeofencingEvent geofencingEvent = GeofencingEvent.<em>fromIntent<\/em>(intent);<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(!geofencingEvent.hasError()) {<\/span>\r\n<span style=\"color: #333399;\"><strong>int <\/strong>transaction = geofencingEvent.getGeofenceTransition();<\/span>\r\n<span style=\"color: #333399;\">List&lt;Geofence&gt; geofences = geofencingEvent.getTriggeringGeofences();<\/span>\r\n<span style=\"color: #333399;\">Geofence geofence = geofences.get(0);<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(transaction == Geofence.<strong><em>GEOFENCE_TRANSITION_ENTER <\/em><\/strong>&amp;&amp; geofence.getRequestId().equals(Constants.GEOFENCE_ID)) {<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"You are inside Rlogical (Geofence Location)\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">} <strong>else <\/strong>{<\/span>\r\n<span style=\"color: #333399;\">Log.<em>d<\/em>(<strong><em>TAG<\/em><\/strong>, <strong>\"You are outside Rlogical (Geofence Location)\"<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>\/\/ Fetch Entering \/ Exiting Detail\r\n<\/em>String geofenceTransitionDetails = getGeofenceTrasitionDetails(transaction, geofences);<\/span>\r\n<span style=\"color: #333399;\">sendNotification(geofenceTransitionDetails);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n<span style=\"color: #333399;\">}}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #003366;\"><strong>Create a detail message with Geofences received<\/strong><\/span><\/h3>\n<pre><span style=\"color: #333399;\"><strong>private <\/strong>String getGeofenceTrasitionDetails(<strong>int <\/strong>geoFenceTransition, List&lt;Geofence&gt; triggeringGeofences) {<\/span>\r\n<span style=\"color: #333399;\"><em>\/\/ get the ID of each geofence triggered\r\n<\/em>ArrayList&lt;String&gt; triggeringGeofencesList = <strong>new <\/strong>ArrayList&lt;&gt;();<\/span>\r\n<span style=\"color: #333399;\"><strong>for <\/strong>(Geofence geofence : triggeringGeofences) {<\/span>\r\n<span style=\"color: #333399;\">triggeringGeofencesList.add(geofence.getRequestId());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">String status = <strong>null<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(geoFenceTransition == Geofence.<strong><em>GEOFENCE_TRANSITION_ENTER<\/em><\/strong>)<\/span>\r\n<span style=\"color: #333399;\">status = <strong>\"Entering \"<\/strong>;<\/span>\r\n<span style=\"color: #333399;\"><strong>else if <\/strong>(geoFenceTransition == Geofence.<strong><em>GEOFENCE_TRANSITION_EXIT<\/em><\/strong>)<\/span>\r\n<span style=\"color: #333399;\">status = <strong>\"Exiting \"<\/strong>;<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>return <\/strong>status + TextUtils.<em>join<\/em>(<strong>\", \"<\/strong>, triggeringGeofencesList);<\/span>\r\n<span style=\"color: #333399;\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #003366;\"><strong>Send a notification<\/strong><\/span><\/h3>\n<pre><span style=\"color: #333399;\"><strong>private void <\/strong>sendNotification(String msg) {<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>\/\/ Intent to start the main Activity\r\n<\/em>Intent notificationIntent = <strong>new <\/strong>Intent(<strong>this<\/strong>, MainActivity.<strong>class<\/strong>);<\/span>\r\n\r\n<span style=\"color: #333399;\">TaskStackBuilder stackBuilder = TaskStackBuilder.<em>create<\/em>(<strong>this<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">stackBuilder.addParentStack(MainActivity.<strong>class<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">stackBuilder.addNextIntent(notificationIntent);<\/span>\r\n<span style=\"color: #333399;\">PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.<strong><em>FLAG_UPDATE_CURRENT<\/em><\/strong>);<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>\/\/ Creating and sending Notification\r\n<\/em><strong>long <\/strong>when = Calendar.<em>getInstance<\/em>().getTimeInMillis();<\/span>\r\n<span style=\"color: #333399;\">NotificationManager notificationManager = (NotificationManager) getSystemService(Context.<strong><em>NOTIFICATION_SERVICE<\/em><\/strong>);<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(notificationManager == <strong>null<\/strong>)<\/span>\r\n<span style=\"color: #333399;\"><strong>return<\/strong>;<\/span>\r\n\r\n<span style=\"color: #333399;\"><strong>if <\/strong>(Build.VERSION.<strong><em>SDK_INT <\/em><\/strong>&gt;= Build.VERSION_CODES.<strong><em>O<\/em><\/strong>) {<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>\/\/ Configure the notification channel.\r\n<\/em>NotificationChannel notifChannel = <strong>new <\/strong>NotificationChannel(msg, msg, NotificationManager.<strong><em>IMPORTANCE_DEFAULT<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">notifChannel.enableLights(<strong>true<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">notifChannel.setLightColor(Color.<strong><em>GREEN<\/em><\/strong>);<\/span>\r\n<span style=\"color: #333399;\">notifChannel.setVibrationPattern(<strong>new long<\/strong>[]{0, 1000, 500, 1000});<\/span>\r\n<span style=\"color: #333399;\">notifChannel.enableVibration(<strong>true<\/strong>);<\/span>\r\n<span style=\"color: #333399;\">notificationManager.createNotificationChannel(notifChannel);<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<span style=\"color: #333399;\">NotificationCompat.Builder noBuilder = <strong>new <\/strong>NotificationCompat.Builder(<strong>this<\/strong>, <strong>\"CH_ID\"<\/strong>)<\/span>\r\n<span style=\"color: #333399;\">.setTicker(msg).setContentTitle(msg).setOngoing(<strong>false<\/strong>).setAutoCancel(<strong>true<\/strong>).setWhen(when)<\/span>\r\n<span style=\"color: #333399;\">.setSound(RingtoneManager.<em>getDefaultUri<\/em>(RingtoneManager.<strong><em>TYPE_NOTIFICATION<\/em><\/strong>))<\/span>\r\n<span style=\"color: #333399;\">.setVibrate(<strong>new long<\/strong>[]{0, 100, 100, 100, 100, 100}).setSmallIcon(R.mipmap.<strong><em>ic_launcher_round<\/em><\/strong>);<\/span>\r\n\r\n<span style=\"color: #333399;\">notificationManager.notify((<strong>int<\/strong>) when, noBuilder.build());<\/span>\r\n<span style=\"color: #333399;\">}<\/span>\r\n\r\n<\/pre>\n<h3><span style=\"color: #003366;\"><strong>In your Manifest add the below<\/strong><\/span><\/h3>\n<p><span style=\"color: #666699;\">Uses Permission<\/span><\/p>\n<pre><span style=\"color: #333399;\">&lt;<strong>uses-permission <\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\"android.permission.ACCESS_FINE_LOCATION\" <\/strong>\/&gt;<\/span>\r\n<span style=\"color: #333399;\">&lt;<strong>uses-permission <\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\"android.permission.ACCESS_COARSE_LOCATION\" <\/strong>\/&gt;<\/span>\r\n<span style=\"color: #333399;\">&lt;<strong>uses-permission <\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\"android.permission.INTERNET\" <\/strong>\/&gt;<\/span><\/pre>\n<p><span style=\"color: #666699;\">Inside Application tag:<\/span><\/p>\n<pre><span style=\"color: #333399;\">&lt;<strong>service\r\n<\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\".GeofenceService\"\r\n<\/strong><strong>android<\/strong><strong>:enabled<\/strong><strong>=\"true\"\r\n<\/strong><strong>android<\/strong><strong>:exported<\/strong><strong>=\"true\" <\/strong>\/&gt;<\/span>\r\n<span style=\"color: #333399;\">&lt;<strong>meta-data\r\n<\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\"com.google.android.gms.version\"\r\n<\/strong><strong>android<\/strong><strong>:value<\/strong><strong>=\"@integer\/google_play_services_version\" <\/strong>\/&gt;<\/span>\r\n\r\n<span style=\"color: #333399;\"><em>&lt;!-- \u00a0\u00a0\u00a0\u00a0\u00a0The API key for Google Maps-based APIs. --&gt;\r\n<\/em>&lt;<strong>meta-data\r\n<\/strong><strong>android<\/strong><strong>:name<\/strong><strong>=\"com.google.android.geo.API_KEY\"\r\n<\/strong><strong>android<\/strong><strong>:value<\/strong><strong>=\"@string\/YOUR_KEY_HERE\" <\/strong>\/&gt;\r\n\r\n<\/span><\/pre>\n<h2><span style=\"color: #003366;\"><strong>How Rlogical Techsoft helps for develops Android Geo-fencing App?<\/strong><\/span><\/h2>\n<p>Rlogical Techsoft is a leading <strong>IT company<\/strong> known for <a href=\"https:\/\/www.rlogical.com\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>web &amp; mobile app development in India.<\/strong><\/a> We have expertise in providing<strong> app solutions based on Geo-location<\/strong> leveraging <strong>Geo-location based mobile app development and solutions.<\/strong><\/p>\n<h3>Contact us with your requirements for development of <a title=\"Android Geo-Fencing\" href=\"https:\/\/www.rlogical.com\/mobile-app-development\/android-app-development\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Geofencing apps in Android<\/strong><\/a>.<\/h3>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Geo fencing? Geofencing is a service that triggers an action when a device enters or exits from the specified predefined location or geographical area for a given radius. This is a location-based service in which a creator of an app or other platform\/software uses GPS, RFID, Wi-Fi or cellular data to send\/trigger messages [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":15010,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[112,38],"tags":[85,1094,225,276,277],"class_list":["post-6994","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-app-development","category-mobile-app-development","tag-android-app-development","tag-geo-fencing-in-an-android-app","tag-geofencing-app","tag-geofencing-app-in-android","tag-hire-android-developers"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/comments?post=6994"}],"version-history":[{"count":11,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6994\/revisions"}],"predecessor-version":[{"id":15008,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6994\/revisions\/15008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/media\/15010"}],"wp:attachment":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/media?parent=6994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/categories?post=6994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/tags?post=6994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}