本文介绍了在 onPause 后 PhoneGap 继续在 Android 上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在应用处于后台时测试 phonegap WebView 是否处于活动状态.

I decided to test if phonegap WebView is live when app is in a background.

function test2(x){
    $('<div></div>', {text: x.cmd}).appendTo($('#list'));
}
$(function(){test2({cmd:"start"});});
function cb_pause(){test2({cmd:"pause"});}
function cb_resume(){   test2({cmd:"resume"});}
function tick(){test2({cmd:"timer" + timer});timer++;setTimeout(tick, 1000);}
document.addEventListener("pause", cb_pause, false);
document.addEventListener("resume", cb_resume, false);
tick();

它在 onPauseonResume 上打印 pauseresume.并每秒打印 timer0timer1 等.

It prints pause and resume on onPause and onResume.and print timer0, timer1, etc every second.

即使我离开应用程序并开始玩游戏,它仍在打印.我知道我可以而且应该在 onPause 中停止计时器,但仍然可以.看起来不对.我预计 android 会冻结 WebView.

It was printing even when I left app and started playing game.I know I can and should stop timer in onPause but still. It looks wrong. I expected android will freeze WebView.

我知道后台线程在后台应用程序被卸载之前不会停止.

I know background thread will not stop if in background app until it is unloaded.

所以问题是:有没有办法在 onPause 上冻结/暂停/卸载 webview 并在 onResume

So question is: Is there way to freeze/suspend/unload webview on onPause and unfreeze/resume/load on onResume

当我说加载时,我的意思是当用户离开应用程序时它应该处于相同的状态.

When I say load I mean it should be in same state when user left app.

当我说冻结时,我的意思是我的代码会暂停,即使它会继续调用 setTimeout

When I say freeze, I mean that my code to will pause even when it will continue to call setTimeout

我想我希望 Android 表现得像 iOS.

I guess I expect Android to behave like iOS.

推荐答案

我迟到了.在 phonegap 中有 KeepRunning 参数在 config.xml

I am late with response. In phonegap there is KeepRunning param in config.xml

您必须将以下行添加到 config.xml

You must add following line to config.xml

<platform name="android">
    ...
    <preference name="KeepRunning" value="false" />

它将调用 pauseTimers() 并暂停所有 WebView.pauseTimers() 不是隐藏方法,不需要反射.

It will call pauseTimers() and pause all WebViews. pauseTimers() is not hidden method and no reflection is needed.

这篇关于在 onPause 后 PhoneGap 继续在 Android 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 10:09