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

问题描述

  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});} {test2({cmd:timer+ timer}); timer ++; setTimeout(tick,1000);}
document.addEventListener(pause,cb_pause,false);
document.addEventListener(resume,cb_resume,false);
tick();

打印暂停和<$ c $在 onPause 和 onResume 上键入 resume 。
并每秒打印 timer0 , timer1 等。



即使我离开应用程序并开始玩游戏,它也在打印。
我知道我可以并应该停止定时器在 onPause 但仍然。它看起来错了。我期望android会冻结WebView。



我知道后台线程在后台应用程序中不会停止,直到它被卸载。



所以问题是:有办法在 onPause 上冻结/暂停/卸载webview,并解冻/恢复/加载 onResume



当我说载入时,我的意思是当用户离开应用程序时应处于相同的状态。



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



I猜猜我期望Android的行为像iOS。

解决方案

我迟到了回复。在phonegap中有 KeepRunning param在 config.xml



您必须在 config.xml

中添加以下行:

 < platform name =android> 
...
< preference name =KeepRunningvalue =false/>

它会调用 pauseTimers()所有WebViews。 pauseTimers()不是隐藏的方法,不需要反射。


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

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

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.

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.

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

I guess I expect Android to behave like iOS.

解决方案

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

You must add following line to config.xml

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

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