{"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":18,"footnotes":""},"categories":[518],"tags":[4845,1832,1489,1491,1482,398,1488,1490],"class_list":["post-14953","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-api","tag-background-location-android","tag-background-location-updates","tag-fused-location-api","tag-google","tag-google-location-android","tag-pendingintent-google-location-api"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Himanshu Kumar Singh\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Background Location Updates on Android | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Background Location Updates on Android | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#article\",\"name\":\"Background Location Updates on Android | TO THE NEW Blog\",\"headline\":\"Background Location Updates on Android\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu-singh\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-07-24T11:36:18+05:30\",\"dateModified\":\"2016-11-30T11:24:57+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#webpage\"},\"articleSection\":\"Android, Android, api, background location android, Background Location updates, fused location api, Google, google location android, PendingIntent google location api\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/android\\\/#listItem\",\"name\":\"Android\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/android\\\/#listItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/android\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#listItem\",\"name\":\"Background Location Updates on Android\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#listItem\",\"position\":3,\"name\":\"Background Location Updates on Android\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/android\\\/#listItem\",\"name\":\"Android\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu-singh\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu-singh\\\/\",\"name\":\"Himanshu Kumar Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/e0265397-05f6-47f9-985d-2d7945f06247_himanshu.singh@tothenew.com.jpg\",\"width\":96,\"height\":96,\"caption\":\"Himanshu Kumar Singh\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/\",\"name\":\"Background Location Updates on Android | TO THE NEW Blog\",\"description\":\"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/background-location-updates-on-android\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu-singh\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/himanshu-singh\\\/#author\"},\"datePublished\":\"2014-07-24T11:36:18+05:30\",\"dateModified\":\"2016-11-30T11:24:57+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Background Location Updates on Android | TO THE NEW Blog","description":"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.","canonical_url":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#article","name":"Background Location Updates on Android | TO THE NEW Blog","headline":"Background Location Updates on Android","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/himanshu-singh\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2014-07-24T11:36:18+05:30","dateModified":"2016-11-30T11:24:57+05:30","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#webpage"},"articleSection":"Android, Android, api, background location android, Background Location updates, fused location api, Google, google location android, PendingIntent google location api"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/android\/#listItem","name":"Android"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/android\/#listItem","position":2,"name":"Android","item":"https:\/\/www.tothenew.com\/blog\/category\/android\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#listItem","name":"Background Location Updates on Android"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#listItem","position":3,"name":"Background Location Updates on Android","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/android\/#listItem","name":"Android"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/himanshu-singh\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/himanshu-singh\/","name":"Himanshu Kumar Singh","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/e0265397-05f6-47f9-985d-2d7945f06247_himanshu.singh@tothenew.com.jpg","width":96,"height":96,"caption":"Himanshu Kumar Singh"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/","name":"Background Location Updates on Android | TO THE NEW Blog","description":"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/himanshu-singh\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/himanshu-singh\/#author"},"datePublished":"2014-07-24T11:36:18+05:30","dateModified":"2016-11-30T11:24:57+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Background Location Updates on Android | TO THE NEW Blog","og:description":"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.","og:url":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Background Location Updates on Android | TO THE NEW Blog","twitter:description":"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"14953","title":null,"description":"This blog walks you through background location updates on Android. A good Android application development company can help you achieve various such Android tricks.","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 15:32:47","updated":"2024-02-29 09:26:53","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/android\/\" title=\"Android\">Android<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tBackground Location Updates on Android\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Android","link":"https:\/\/www.tothenew.com\/blog\/category\/android\/"},{"label":"Background Location Updates on Android","link":"https:\/\/www.tothenew.com\/blog\/background-location-updates-on-android\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14953","targetHints":{"allow":["GET"]}}],"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}]}}