2

I have an App which constantly polls for location when its in travelling mode. My issue is that once the screen is locked, the App can no longer access the geolocation from the phone.

I have managed to find this plugin but it requires I purchase it for the capability to work in Android. http://shop.transistorsoft.com/pages/cordova-background-geolocation-premium

Does anyone know if there is a free option that i can use to get location to successfully poll in an Ionic / Cordova application while the screen is locked?

SM3RKY
  • 1,548
  • 1
  • 14
  • 27

2 Answers2

3

Have you looked at NG-Cordova?

first add ng-cordova to your project:

bower install ngCordova

or

<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>

Then inject it:

angular.module('myApp', ['ngCordova'])

Here is a plugin you could try: http://ngcordova.com/docs/plugins/backgroundGeolocation/

just install the plugin:

cordova plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git

then bind it to a controller:

module.controller('MyCtrl', function($scope, $cordovaBackgroundGeolocation) {



    var options = {
        // https://github.com/christocracy/cordova-plugin-background-geolocation#config
      };

      document.addEventListener("deviceready", function () {

        // `configure` calls `start` internally
        $cordovaBackgroundGeolocation.configure(options)
        .then(
          null, // Background never resolves
          function (err) { // error callback
            console.error(err);
          },
          function (location) { // notify callback
            console.log(location);
          });


        $scope.stopBackgroundGeolocation = function () {
          $cordovaBackgroundGeolocation.stop();
        };

      }, false);
    });
Jess Patton
  • 2,476
  • 1
  • 15
  • 26
  • Thanks @Jess, but it seems they have cleared out that repo, and changed the README.md file to point to the paid version I originally linked. If I ran that plugin install, I imagine it would now just download the lisence and readme files? – SM3RKY Sep 03 '15 at 23:32
  • 1
    well it looks like everything is a fork of the paid for one, I don't know how comfortable you are with this but if you think the time is worth saving the money you could write your own plugin https://cordova.apache.org/docs/en/4.0.0/guide_hybrid_plugins_index.md.html – Jess Patton Sep 03 '15 at 23:38
  • Or you can use already forked version: https://github.com/srom/cordova-plugin-background-geolocation – Gajotres Sep 04 '15 at 08:14
2

Another option is to use a partial wakelock on Android to keep your app alive while in the background (screen off or switched out of foreground). You'd need to do this via a plugin, but it would have the same effect as a background service, keeping you app alive to receive location updates while in the background.

See my old answer here for the source code for a Cordova 2.0 plugin (it will need updating for Cordova 3+).

Community
  • 1
  • 1
DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • 1
    There's now a version of this plugin which supports partial wakelocks and is available via GitHub: https://github.com/boltex/cordova-plugin-powermanagement – DaveAlden Oct 14 '15 at 07:46
  • @DaveAlden sir i am using ionic3.9 please tell me how to use in the ionic3 ? – Kapil Soni Apr 21 '19 at 06:20