在 onPause 之后 PhoneGap 继续在 Android 上运行 [英] PhoneGap keep running on Android after onPause

查看:35
本文介绍了在 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 中,config.xml

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

您必须在 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 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆