user1688401
user1688401

Reputation: 1871

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

Upvotes: 4

Views: 7653

Answers (4)

tf z
tf z

Reputation: 61

this is my answer in here, https://stackoverflow.com/a/78470208/6631835 .

Just for Android:

if you don't want use this plugin : cordova-plugin-background and you have power change your android app code. Here is a method, and the end i tell you why cordova-plugin-background don't work (this is my guess, and must read it,if you use this answer).

this is android code.

// first you need create android `jsbridge`
  webView.addJavascriptInterface(InJavaScriptLocalObj(), "android")
      @Suppress("unused")
  inner class InJavaScriptLocalObj {
/**
* @ script you want to run function in js code
  @ interval time unit is millisecond
*/
 @JavascriptInterface
 fun setInterval(script: String, interval: Long): Int {
            // TODO check need to cancel
            if (hashMap.size > 0) {
                hashMap.forEach { it.value.cancel() }
                hashMap.clear()
            }
            val jobSeek = scopeMain.launch {
                while (isActive) {
                    [email protected] {
                       //Run the script in here.
                        webView.evaluateJavascript(script),
                            object : ValueCallback<String?> {
                                override fun onReceiveValue(value: String?) {
                                    if (!value.isNullOrEmpty()){
                                
                                    }
                                }
                            })
                    }
                    delay(interval)
                }
            }
            val id = hashMap.size + 1

            hashMap[id] = jobSeek
            return id
        }

        @JavascriptInterface
        fun clearInterval(id: Int) {
            hashMap[id]?.cancel()
            hashMap.remove(id)
        }
  }

this is JS code


android.setInterval(`interval()`)

function interval(){}

// if you use vue or other frame work,you need do this

window.interval=interval()

// My function in pineapple,so i do this
window.interval=useCounterStore().interval()

But you need to pay attention:

In background,android webview can't update view, can't request network, just can run that function by native called.


why cordova-plugin-background don't work?

you need to tell the user set app IgnoringBatteryOptimizations.

Here is a method https://stackoverflow.com/a/62088633/6631835

Upvotes: 0

ptt
ptt

Reputation: 111

Your problem the background plugin does not work, because it only activated when you switch / pause the app.

cordova.plugins.backgroundMode.enable();
alert(cordova.plugins.backgroundMode.isActive());

Your code above cannot return isActive() because the app does not pause / go to background at the time called.

Upvotes: 0

richard barakat
richard barakat

Reputation: 1

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

Upvotes: 0

Uniphonic
Uniphonic

Reputation: 875

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

Upvotes: 2

Related Questions