navigator.geolocation.getCurrentPosition() when screen is locked

坚强是说给别人听的谎言 提交于 2019-12-02 00:15:51
DaveAlden

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+).

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);
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!