[英]ionic + cordova plugin and background mode
我正在用離子技術為汽車駕駛員制作一個應用程序。 該應用程序每隔一分鍾獲取一次坐標,並將其寫入遠程服務器,這有助於我跟蹤路線並在Google地圖上顯示汽車。
我使用Cordova插件,除屏幕關閉或應用程序進入后台外,它均能正常工作。 我安裝了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();
})
我知道這是一個舊線程,但是我確實設法通過以下方法使它起作用:
cordova.plugins.backgroundMode.on('enable', function(){
//your code here, will execute when background tasks is enabled
loop();
});
function loop(){
console.log("loop");
$timeout(loop, 1000);
}
cordova.plugins.backgroundMode.enable();
@Cristian,啟用插件后,您應該調用該函數或在此函數中編寫邏輯。
cordova.plugins.backgroundMode.onactivate = function() {
// your logic here
// or call any other service, factory function
};
您是否要制作一個每隔x分鍾執行一次操作的應用程序,即使該應用程序在后台和/或屏幕關閉時也是如此。...嘗試過backgound-mode插件,但僅在插入電話后才能可靠地運行...您是否找到了解決方案? 使用cordova命令行實用程序安裝插件:
$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git
這是我與后台模式插件一起使用的方式,因此該應用程序永遠不會在后台並且始終作為服務運行...:
if( ionic.Platform.isAndroid() ){
cordova.plugins.backgroundMode.enable();
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
window.powerManagement.setReleaseOnPause(false, function() {
console.log('setReleaseOnPause successfully');
}, function() {
console.log('Failed to set');
});
}
最后一步當您完成服務時,請停用插件
cordova.plugins.backgroundMode.disable()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.