Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

cordova - PhoneGap keep running on Android after onPause

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...