gpt4 book ai didi

cordova - 使用 AngularJS 和 Ionic 访问 PhoneGap 中运行的后台进程

转载 作者:行者123 更新时间:2023-12-02 03:34:24 25 4
gpt4 key购买 nike

我在使用 angularjs 和 ionic 的 phonegap 项目中遇到地理定位后台进程 ( https://github.com/christocracy/cordova-plugin-background-geolocation) 的问题。该过程运行良好,但有时我无法停止。
如果应用程序在前台运行,则一切正常。但是当我启动后台服务然后按下我的设备 (Samsung Galaxy S2) 上的主页按钮时,进程在后台运行 -> 正确。现在我重新打开应用程序并尝试停止后台进程,但此时它不起作用。

看来我没有正确的流程实例。

这是我的代码:

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:也许你知道解决方案?

最佳答案

我现在使用这个功能:

function stopService(){
var bgGeoService = window.plugins.backgroundGeoLocation
bgGeoService.stop()
}

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

希望这对您有所帮助。

关于cordova - 使用 AngularJS 和 Ionic 访问 PhoneGap 中运行的后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com