简体   繁体   中英

setInterval functions not running when app in background

I'm developing a simple app with phonegap which every 30 seconds send user current coordinates to my database via ajax call.It works very well

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

If the app is in the foreground there is no problem and everything works fine, but if I open google maps app with this code

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

My app goes in the background(my app and google map app work separately.Two apps work at same time) and the interval function is not executed any more.

Is it possible to have the javascript code (a timer function) executing in the background with phonegap?

Edited:

I use cordova-plugin-background-mode( https://github.com/katzer/cordova-plugin-background-mode ) but it still does not work.And alert give false.What is wrong with this?

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

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

I was looking for a solution to the same problem, and I think I just found one, though I haven't been able to fully try this out yet, I thought I'd post the answer here anyways, so you can try it out too.

One solution would be to set the timer in native code, instead of JavaScript, and there seems to be a Cordova plugin for just such a purpose:

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

After installing the Cordova plugin, you can use it like this (pasted from the documentation):

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

if you are still looking for an answer for this issue , or it can help someone else: im sure that you are facing this problem in android so to resolve this you should add this piece of code after you add background-mode cordova plugin to your project :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM