{"id":83,"date":"2016-01-22T20:32:01","date_gmt":"2016-01-22T19:32:01","guid":{"rendered":"http:\/\/filiz.it\/?p=83"},"modified":"2020-11-19T16:33:24","modified_gmt":"2020-11-19T15:33:24","slug":"android-tutorial-geofence","status":"publish","type":"post","link":"http:\/\/filiz.it\/index.php\/2016\/01\/22\/android-tutorial-geofence\/","title":{"rendered":"Android Tutorial: Geofence"},"content":{"rendered":"<p>This is a continuation of the <strong>&#8222;Adding Google Maps with a MapView&#8220;<\/strong> tutorial. I added the Google API&nbsp;to display maps&nbsp;on a Android App..now i\u00b4m going to add geofencing.<\/p>\n<p>You can download the SourceCode&nbsp;on <a href=\"https:\/\/github.com\/FilHazer\/Maps\">GitHub<\/a>.<\/p>\n<h4><\/h4>\n<h4><\/h4>\n<h4><strong>Step 1: &nbsp;Connect to&nbsp;Google API Client&nbsp;<\/strong><\/h4>\n<p>For the geofence implementation we need a connection&nbsp;to the Google Api Client. This&nbsp;Client allows us to use the Google Services like Search, Gmail, Translate and so on..<\/p>\n<p>I\u00b4m going to create a new class called <strong>GeofenceController<\/strong> and implement all the needed methods for our case. Lets begin&nbsp;with starting&nbsp;and stoping&nbsp;the Google API&nbsp;Client..<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/9ca4bf6ab55db0efe9b7.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/9ca4bf6ab55db0efe9b7\">Gist<\/a>.<\/noscript><\/div>\n<p>As you can see we&nbsp;need two callbacks to start the client. One for successfull and one for failer connection. I create them separately.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/88fead375134ab55a02b.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/88fead375134ab55a02b\">Gist<\/a>.<\/noscript><\/div>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/0d0930092b8915146fab.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/0d0930092b8915146fab\">Gist<\/a>.<\/noscript><\/div>\n<p>Maybe you are wondering what that means..<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/2ad80eb38f7139a09a36.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/2ad80eb38f7139a09a36\">Gist<\/a>.<\/noscript><\/div>\n<p>For&nbsp;the connection&nbsp;we need the ApplicationContext. In this case i create a new application class to get access to the context and insert it in the<strong> GoogleApiClient.Builder<\/strong>.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/6fbe24a6560967e3adcf.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/6fbe24a6560967e3adcf\">Gist<\/a>.<\/noscript><\/div>\n<p>Don\u00b4t forget to add it in the <strong>AndroidManifest<\/strong><\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/1775f6bad5f7154cda05.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/1775f6bad5f7154cda05\">Gist<\/a>.<\/noscript><\/div>\n<p>The last thing that left is just to start and stop the client through the <strong>MainActivity&nbsp;<\/strong>with the callbacks we created.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/2fe095d9c0211e3f923a.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/2fe095d9c0211e3f923a\">Gist<\/a>.<\/noscript><\/div>\n<p>If you run and build the app you should be able to see the success connection in the logs.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 2: Adding Geofence<\/strong><\/h4>\n<p>To add geofence we need to create a new geofence object&nbsp;with the&nbsp;&nbsp;<strong>GeofenceBuilder<\/strong>. This is crux to monitor and trigger the geofence events.<\/p>\n<p>I\u00b4m going to do that separately in a class. I also add the method&nbsp;<strong>geofence()<\/strong> where we are able to get the new created geofence object. What you need is a <strong>id<\/strong>, <strong>name<\/strong>, <strong>latitude<\/strong>&nbsp;and <strong>longitude<\/strong> of the favored location and the <strong>radius<\/strong> when the event should be triggered. You can set different transitionTypes for your geofence. In our case i add all 3 types.<\/p>\n<p>Geofence.GEOFENCE_TRANSITION_ENTER = triggered if location entered<\/p>\n<p>Geofence.GEOFENCE_TRANSITION_EXIT = triggered if location&nbsp;leaved<\/p>\n<p>Geofence.GEOFENCE_TRANSITION_DWELL = triggered if&nbsp;location passed for a while. For this event you have to set a <strong>LoiteringDelay.&nbsp;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/8f9f50c1cccd70c1a18f.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/8f9f50c1cccd70c1a18f\">Gist<\/a>.<\/noscript><\/div>\n<p>Let\u00b4s amplify the <strong>GeofenceController<\/strong> with a new method <strong>addGeofence<\/strong>(). With this method we create a new geofence and start the GoogleApiClient with our callbacks. In this case i\u00b4m going to create a new callback<strong> AddGeoConnectionCallBack<\/strong>&nbsp;where i add a new service to trigger the events.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/5dbece94a7504de286e1.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/5dbece94a7504de286e1\">Gist<\/a>.<\/noscript><\/div>\n<p>Create a new intent to handle the events. We need the GoogleApiClient and the <strong>GeofenceRequest<\/strong>. The Request specifies the list of geofence to be monitored.<\/p>\n<p>&nbsp;<\/p>\n<p>Adding a new method <strong>getAddGeofencingRequest()<\/strong> in <strong>GeofenceController<\/strong>..<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/74ed13a0eb6836efa8d3.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/74ed13a0eb6836efa8d3\">Gist<\/a>.<\/noscript><\/div>\n<p>Create a new <strong>IntentService<\/strong>.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/7bc51e90f630421e7953.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/7bc51e90f630421e7953\">Gist<\/a>.<\/noscript><\/div>\n<p>Configure <strong>AndroidManifest.xml.&nbsp;<\/strong><\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/FilHazer\/ce2d4e7a4ce9efcbde6f.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/FilHazer\/ce2d4e7a4ce9efcbde6f\">Gist<\/a>.<\/noscript><\/div>\n<p>Just&nbsp;add this&nbsp;line<strong> googleMap.setMyLocationEnabled(true)<\/strong> into the <strong>onMapReady()<\/strong> method.&nbsp;You should be able to see your location. Don\u00b4t forget to setup the marker otherwise you have to scroll in the map&nbsp;to your location to see the blue point \ud83d\ude42<\/p>\n<p>Build an run..in the logs you can see that the geofence successfully added with your current location and the event &#8222;ENTER&#8220;&nbsp;is fired.<\/p>\n<p>Check out the SourceCode on <a href=\"https:\/\/github.com\/FilHazer\/geofencing\">GitHub<\/a><\/p>\n<p><strong>Note<\/strong>: Just tested on API Level 22 and 21<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a continuation of the &#8222;Adding Google Maps with a MapView&#8220; tutorial. I added the Google API&nbsp;to display maps&nbsp;on a Android App..now i\u00b4m going to add geofencing. You can download the SourceCode&nbsp;on GitHub. Step 1: &nbsp;Connect to&nbsp;Google API Client&nbsp; For the geofence implementation we need a connection&nbsp;to the Google Api Client. This&nbsp;Client allows us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[5,4],"_links":{"self":[{"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/posts\/83"}],"collection":[{"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":54,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":194,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions\/194"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/media\/139"}],"wp:attachment":[{"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/filiz.it\/index.php\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}