gpt4 book ai didi

android - ionic + cordova 插件和后台模式

转载 作者:行者123 更新时间:2023-11-29 15:45:21 25 4
gpt4 key购买 nike

我正在为汽车司机制作一个带有 ionic 的应用程序。该应用程序每隔一分钟获取一次坐标并将其写入远程服务器,这有助于我跟踪路线并在谷歌地图上显示汽车。

我使用 Cordova 插件,它工作正常,除非屏幕关闭或应用程序进入后台。我安装了 katzer cordova-plugin-background-mode,当应用程序进入后台时我看到消息:应用程序现在在后台,插件通知我但没有别的,应用程序停止了! .

没有数据发送到远程服务器,当我恢复应用程序时一切恢复正常,我使用的是 Android 平台。我该如何解决?

app.js 代码

<p></p>

<pre><code>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();

});
})
.....
</code></pre>

<p></p>

controllers.js

<p></p>

<pre><code>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();

})
</code></pre>

<p></p>

最佳答案

我知道这是一个旧线程,但我确实设法让它工作了,方法是:

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();

关于android - ionic + cordova 插件和后台模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880400/

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