简体   繁体   中英

Completely stop an android webview

I have an android webview and allow the user to use their own javascript inside a window.onload callback (its completely isolated and I personally don't see a security risk). The problem occurs if something like the following is entered

while(true){
}

In the java side of things, I start a timer and after about 5 seconds if JavaScript is still going I try to stop the webview. I have tried various things:

webView.stopLoading();
webView.loadData("", "text/html", null);
webView.freeMemory();
webView.pauseTimers();
webView.destroy();
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webView, (Object[]) null);

each one getting a little more desperate :). Nothing works and a thread continues to use up the processor until I quit the app. Does anyone know how I can stop it correctly?

Any help is much appreciated, Ian

Have you tried disabling JavaScript before destroying the WebView? It seems like the JavaScript interpreter is outliving your WebView, but perhaps if you tell it to stop executing, then you will solve your problem. I'd try using this at the top of your timed method:

webView.getSettings().setJavaScriptEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM