Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first thing to say is that creating a Phonegap app that receives location updates while running in the background is entirely possible, but not trivial. I've done it myself and released apps on both the Android and iOS platforms.</p> <p>If you need accurate and regular position updates, I'd suggest using the GPS receiver on the target devices. In Phonegap, you can do this setting the "highAccuracy" flag when requesting position updates. The watchPosition() function will deliver new position information as and when the device receives updates from the GPS receiver, so you use it something like this:</p> <pre><code>navigator.geolocation.watchPosition(successCallback, errorCallback, { enableHighAccuracy: true, timeout: 10000, maximumAge: 0 }); </code></pre> <p>See the <a href="http://docs.phonegap.com/en/2.9.0/cordova_geolocation_geolocation.md.html#Geolocation" rel="nofollow noreferrer">Phonegap geolocation API documentation</a> for more details (note the permissions that are required to make this work on Android and iOS).</p> <p>To keep your app running in the background on either Android or iOS you will need to setup their respective native development environments: Eclipse for Android, XCode for iOS. You will not be able to use <a href="https://build.phonegap.com/" rel="nofollow noreferrer">Phonegap Build</a> because custom settings are needed in both cases to make it work. See the <a href="http://docs.phonegap.com/en/2.9.0/guide_getting-started_index.md.html#Platform%20Guides" rel="nofollow noreferrer">Phonegap platform guides</a> for how to do this.</p> <p>To keep your app running in the background on Android, you either need to write a custom Android service, or you could do what I did and use a Phonegap plugin to acquire a "partial wakelock" (<a href="http://developer.android.com/reference/android/os/PowerManager.html" rel="nofollow noreferrer">see here</a>) to keep your app running in the background and receive position updates while the screen is off. Note that the <a href="https://github.com/phonegap/phonegap-plugins/tree/master/Android/PowerManagement" rel="nofollow noreferrer">original version</a> of this plugin is out-of-date and doesn't work with more recent versions of Phonegap, and also doesn't support partial wakelocks. However, I've updated and extended it for my own use: you can find the source code for it in my answer to <a href="https://stackoverflow.com/questions/17250808/plugin-phonegap-power-management/17251197#17251197">this question</a>.</p> <p>To keep your app running in the background on iOS, you need to do things slightly differently; you don't need a plugin, just a project setting. </p> <p>You can either manually edit the project .plist and add the key “UIBackgroundModes” key with a value of “location” or, with your project open in XCode, add the "Required Background Modes" key with a value of "App registers for location updates". This will cause iOS to trigger the JS callback function you have registered with watchPosition() each time a location update is received. <a href="http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html" rel="nofollow noreferrer">See here</a> for more about iOS project keys and <a href="http://www.raywenderlich.com/29948/backgrounding-for-ios" rel="nofollow noreferrer">background modes</a>.</p> <p>Hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload