【问题标题】:setInterval functions not running when app in background当应用程序在后台运行时,setInterval 函数不运行
【发布时间】:2017-09-12 15:00:56
【问题描述】:

我正在开发一个带有 phonegap 的简单应用程序,它每 30 秒通过 ajax 调用将用户当前坐标发送到我的数据库。效果很好

$(document).ready(function() {
    setInterval(function() {
            SetLocationUpdates();
        }, 30000);
         });

如果应用程序在前台,则没有问题,一切正常,但如果我使用此代码打开谷歌地图应用程序

<div><a href="geo:41.897096,27.036545">Open maps app</div>

我的应用程序进入后台(我的应用程序和谷歌地图应用程序分别工作。两个应用程序同时工作)并且不再执行间隔功能。

是否可以通过 phonegap 在后台执行 javascript 代码(计时器功能)?

已编辑:

我使用了 cordova-plugin-background-mode(https://github.com/katzer/cordova-plugin-background-mode) 但它仍然不起作用。并且警报给出错误。这有什么问题?

document.addEventListener('deviceready', function () {

    cordova.plugins.backgroundMode.enable();
    alert(cordova.plugins.backgroundMode.isActive());
}, false);

【问题讨论】:

  • Javascript 计时器通常在现代浏览器中在它们运行的​​上下文(即窗口)失去焦点时暂停。听起来它在 PhoneGap 中的工作方式有些相同?
  • 我应该怎么做?我应该使用网络工作者还是有什么解决方案?
  • 不确定,也许其他人有解决 PhoneGap 的方法?
  • 我尝试了 cordova-plugin-background-mode 但它仍然不起作用。我编辑了问题并将此信息添加到问题的末尾
  • 您好,您解决了吗?我也有同样的问题。

标签: javascript google-maps timer phonegap-plugins phonegap


【解决方案1】:

我一直在寻找解决同一问题的方法,但我想我刚刚找到了一个,虽然我还没有完全尝试过,但我想我还是会在这里发布答案,所以你可以也试试看。

一种解决方案是在本机代码中设置计时器,而不是 JavaScript,并且似乎有一个 Cordova 插件用于此目的:

https://github.com/dukhanov/cordova-background-timer

安装 Cordova 插件后,您可以像这样使用它(从文档中粘贴):

var eventCallback = function() {
    // timer event fired
}

var successCallback = function() {
    // timer plugin configured successfully
}

var errorCallback = function(e) {
    // an error occurred
}

var settings = {
    timerInterval: 60000, // interval between ticks of the timer in milliseconds (Default: 60000)
    startOnBoot: true, // enable this to start timer after the device was restarted (Default: false)
    stopOnTerminate: true, // set to true to force stop timer in case the app is terminated (User closed the app and etc.) (Default: true)

    hours: 12, // delay timer to start at certain time (Default: -1)
    minutes: 0, // delay timer to start at certain time (Default: -1)
}

window.BackgroundTimer.onTimerEvent(eventCallback); // subscribe on timer event
// timer will start at 12:00
window.BackgroundTimer.start(successCallback, errorCallback, settings);

【讨论】:

  • 谢谢,但是这个插件只适用于安卓系统,对吧?
  • 是的,目前仅适用于 Android,尽管将 iOS 添加到插件中可能不会太难。如果我正在研究的项目获得批准,我就有机会自己做。
  • 我怎样才能联系到你?
  • 如果我最终将 iOS 平台添加到插件中,或者如果我让它以其他方式工作,我会在这里发帖。
  • 好的,谢谢。我注意到这个问题与另一个插件有关:cordova-plugin-tts。我打开了一个案例:github.com/vilic/cordova-plugin-tts/issues/59 如果我在计时器功能中启用后台模式,我会在没有插件的情况下在 bg 模式下运行 setinterval。但如果 TTS 之前运行过,它会打破后台模式。
【解决方案2】:

如果您仍在寻找此问题的答案,或者它可以帮助其他人: 我确定你在 android 中遇到了这个问题,所以要解决这个问题,你应该在将 background-mode cordova 插件添加到项目后添加这段代码:

this.backgroundMode.enable();
   this.backgroundMode.on('activate').subscribe(()=>{
       this.backgroundMode.disableWebViewOptimizations(); 
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2014-01-10
    • 2011-09-11
    • 2020-12-12
    相关资源
    最近更新 更多