首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用AngularJS和Ionic访问PhoneGap中运行的后台进程

用AngularJS和Ionic访问PhoneGap中运行的后台进程
EN

Stack Overflow用户
提问于 2014-07-06 22:58:36
回答 1查看 4.9K关注 0票数 3

我在一个使用angularjs和ionic的phonegap项目中遇到了一个地理定位后台进程(https://github.com/christocracy/cordova-plugin-background-geolocation)的问题。进程运行得很好,但有时我无法停止它。

如果应用程序在前台运行,则一切正常。但是,当我启动后台服务,然后按下我的设备(三星盖乐世S2)上的主页按钮,该进程在后台运行->正确。现在我重新打开应用程序并尝试停止后台进程,但此时它不起作用。

似乎我没有这个过程的正确实例。

这是我的代码:

代码语言:javascript
复制
var bgGeo;

angular.module('starter.services', [])
.factory('LocationService', function($http, $location){
    function startBackgroundLocation() {
        var gpsOptions = {
            enableHighAccuracy : true,
            timeout: 10000,
            maximumAge: 5000
        };
        navigator.geolocation.getCurrentPosition(function(location) {
            console.log('Location from Phonegap');
        },
        function (error){
            alert('error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
        },gpsOptions);


        bgGeo = window.plugins.backgroundGeoLocation;

        /**
        * This would be your own callback for Ajax-requests after POSTing background geolocation to your server.
        */
        var yourAjaxCallback = function(response) {
            ////
            // IMPORTANT:  You must execute the #finish method here to inform the native plugin that you're finished,
            //  and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
            // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
            //
            //
            bgGeo.finish();
        };

        /**
        * This callback will be executed every time a geolocation is recorded in the background.
        */
        var callbackFn = function(location) {
            alert('[js] BackgroundGeoLocation callback:  ' + location.latitudue + ',' + location.longitude);
            // Do your HTTP request here to POST location to your server.
            //
            //

            // This is never called in Android
            yourAjaxCallback.call(this);
        };

        var failureFn = function(error) {
            alert('BackgroundGeoLocation error');
        }

        // BackgroundGeoLocation is highly configurable.
        bgGeo.configure(callbackFn, failureFn, {
            url: apiUrl + '/position/', // <-- only required for Android; ios allows javascript callbacks for your http
            params: {                                               // HTTP POST params sent to your server when persisting locations.
               auth_token: localStorage.getItem('gcmToken')
            },
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 30,
            debug: true // <-- enable this hear sounds for background-geolocation life-cycle.
        });

        // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
        // wenn der Service bereits läuft, nicht mehr starten
        bgGeo.start();
    }

    function stopBackgroundLocation(){
        bgGeo.stop();
    }

    return {
        start: function(){
            init();
        },
        stop : function () {
            stopBackgroundLocation();
        }
    }
}

当我在应用程序恢复后调用LocationService.stop()时,后台地理定位没有停止。

有没有人知道哪里出了问题,或者我有什么需要补充的?谢谢。

@Aleks:也许你知道解决方案?

EN

回答 1

Stack Overflow用户

发布于 2014-08-12 19:55:01

我现在使用这个函数:

代码语言:javascript
复制
function stopService(){
  var bgGeoService = window.plugins.backgroundGeoLocation
  bgGeoService.stop()
}

而且它似乎起作用了。但还有另一种方法,请参阅此链接:https://github.com/christocracy/cordova-plugin-background-geolocation/issues/48

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24597137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档