我正在爲汽車司機開發一個離子應用程序。該應用程序每隔一分鐘就會讀取一次座標並將其寫入遠程服務器,這有助於我跟蹤路線並在Google地圖上顯示汽車。離子+科爾多瓦插件和背景模式
我使用科爾多瓦插件,除非屏幕關閉或應用程序進入後臺,否則它工作正常。我安裝了katzer cordova-plugin-background-mode,當應用程序進入後臺時,我看到消息:應用程序現在處於後臺,插件通知我但沒有其他任何事情,應用程序停止! 。
沒有數據發送到遠程服務器,當我恢復應用程序一切恢復正常時,我使用Android平臺。我該如何解決?
app.js代碼
angular.module('starter', ['ionic', 'ngCordova','LocalStorageModule', 'starter.controllers', 'starter.services'])
.run(function ($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleLightContent();
}
cordova.plugins.backgroundMode.enable();
});
})
.....
controllers.js
angular.module('starter.controllers', [])
.controller('GeoCtrl', function($scope, $cordovaGeolocation, $cordovaNetwork, $http, $interval) {
var reloadCoordinates = function() {
var watchOptions = {
timeout : 10000,
enableHighAccuracy: false // may cause errors if true
};
var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
null,
function(err) {
// error
},
function(position) {
//Latitudine e Longitudine
var lat = position.coords.latitude
var lon = position.coords.longitude
$scope.latitude = lat
$scope.longitude = lon
$http.post('http://192.168.1.2/get-data.php', { "lat": lat, "lon" : lon }).then(function(resp) {
console.log('Success Lat:'+resp.data.lat+' Lon:'+resp.data.lon);
watch.clearWatch();
}, function(err) {
console.error('ERR', err);
// err.status will contain the status code
})
});
};//end reloadCoordinates
$interval(reloadCoordinates, 60000);
reloadCoordinates();
})
請參閱此[鏈接](http://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012)當我使用媒體播放器播放歌曲時,解決了背景活動 –
謝謝Anil,我查了一下,並且看到了背景模式插件工作正常,基於哪個智能手機按鈕被按下,例如在我的單頁應用程序中:後退按鈕 - >退出應用程序;主頁按鈕 - >應用程序進入後臺,圖標出現在狀態欄中。在這兩種情況下,插件顯示的消息:應用程序運行在後臺,這可以混淆驅動程序時,他們將使用應用程序 – Cristian
好吧,你可以做一件事情,當你想使用地圖激活上面的插件代碼 –