{"id":14953,"date":"2014-07-24T11:36:18","date_gmt":"2014-07-24T06:06:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14953"},"modified":"2016-11-30T11:24:57","modified_gmt":"2016-11-30T05:54:57","slug":"background-location-updates-on-android","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/","title":{"rendered":"Background Location Updates on Android"},"content":{"rendered":"<p style=\"text-align: justify\">In my\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/googles-fused-location-api-for-android\/\" target=\"_blank\">previous blog<\/a>,\u00a0I mentioned my first task about removing the <del><em>AlarmManager\/LocationManager<\/em><\/del> approach we were using and replace it with new Location APIs that were announced at Google I\/O last year.<\/p>\n<p style=\"text-align: justify\"><strong>Note:<\/strong> If you are unaware about using\u00a0Google location api in the foreground, Please firstly read the <a href=\"http:\/\/www.tothenew.com\/blog\/googles-fused-location-api-for-android\/\">previous blog<\/a>.<\/p>\n<p style=\"text-align: justify\">This method was working well while the <a title=\"android application development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\" target=\"_blank\">android application<\/a> was in the foreground, but was not updating the location while in the background.<\/p>\n<p style=\"text-align: justify\">Confused, I went to the docs to find what was going wrong. I was using the <em>LocationClient.requestLocationUpdates()<\/em> version which takes in a <em>LocationListener<\/em>. The documentation states <strong>\u201cThis method is suited for the foreground use cases. For background use cases, the PendingIntent version of the method is recommended\u201d<\/strong>.<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: x-large\"><span class=\"highlight\">WAY TO USE<\/span><\/span><\/p>\n<p style=\"text-align: justify\"><strong>1.<\/strong> Use the PendingIntent in our code.<\/p>\n<p>[java]<br \/>\n@Override<br \/>\npublic void onConnected(Bundle bundle) {<br \/>\nIntent intervalIntent = new Intent(this, IntervalPendingIntentService.class);<br \/>\nPendingIntent intervalPendingIntent = PendingIntent.getService(this, 1,intervalIntent, PendingIntent.FLAG_CANCEL_CURRENT);<br \/>\nmLocationClient.requestLocationUpdates(mLocationRequest, intervalPendingIntent);<br \/>\n&#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify\"><strong>2.<\/strong> We can get location in the started component like :<\/p>\n<p>[java]<br \/>\npublic class IntervalPendingIntentService extends IntentService{<br \/>\n@Override<br \/>\n\tprotected void onHandleIntent(Intent intent)<br \/>\n\t{<br \/>\n\t\t\tLocation location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);<br \/>\n\t\t\tif(location !=null)<br \/>\n\t\t\t{<br \/>\n\t\t\t\/\/our location based code<br \/>\n\t\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify\"><strong>Note:<\/strong> android.location.Location class implements <strong>Parcelable<\/strong> interface by which we can send\/receive whole Location object to\/from any android component through Intent. e.g. :<\/p>\n<p>[java]<br \/>\nIntent intent = new Intent();<br \/>\nintent.putExtra(&quot;loc&quot;, location);<br \/>\ngetApplicationContext().sendBroadcast(intent);<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify\">Changing\u00a0the new API, we receive location updates even when the app is in the background. This makes the api code is more manageable and we also get an advantage of the API\u2019s battery-saving optimization.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my\u00a0previous blog,\u00a0I mentioned my first task about removing the AlarmManager\/LocationManager approach we were using and replace it with new Location APIs that were announced at Google I\/O last year. Note: If you are unaware about using\u00a0Google location api in the foreground, Please firstly read the previous blog. This method was working well while the [&hellip;]<\/p>\n","protected":false},"author":126,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[518],"tags":[4845,1832,1489,1491,1482,398,1488,1490],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14953"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/126"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14953"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14953\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}