我在使用angularjs和离子的phonegap项目中的地理位置后台进程(https://github.com/christocracy/cordova-plugin-background-geolocation)存在问题。这个过程运行良好,但有时我无法阻止它。
如果应用程序在前台运行,它都可以正常工作。但是当我启动后台服务,然后按下我的设备上的主页按钮(samsung galaxy s2)时,该过程在后台运行 - >正确。现在我重新打开应用程序并尝试停止后台进程,但目前它不起作用。
看来我没有正确的过程实例。
这是我的代码:使用angularjs和离子在phonegap上运行后台进程的访问
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:也许你知道一个解决方案?
Nowbody有一个解决方案或至少有一个想法或提示?请这真是重要的,因为它是为我的考试。谢谢! – sbr11