问题
I'm building a webapp for Android smartphones that runs with the OS internet browser. the main interface is to input data. the data is added to a queue (android 1.5: gears, android 2.x: html5). Each 5 minutes (using setTimeout), the script looks if an internet connection is active, and if so, sends all the queue to the server.
If the phone is plugged on the wall and the webpage is ontop, the timeout works. if the browser is minimized or another application runs on top of it, the timeout doesn't work. if the phone is in sleep mode it doesn't work either.
Can only native apps runs in background?
回答1:
can only native apps runs in background?
Certainly, I would not expect the browser to be waking up the device, for your sleep mode scenario. Apparently, based on your symptoms, they pause all Javascript threads when the browser itself is paused. That is not terribly shocking, given the battery problems that leaving those scripts running might cause.
回答2:
You could also base it on system time, rather than just setTimeout. That way it would at least run when they returned focus to the browser.
来源:https://stackoverflow.com/questions/2851488/android-browser-javascript-events-when-minimised