Ami*_*ski 34 multithreading android webview
我使用WebView在我们的某个应用程序的活动中显示一些互联网内容.
问题是,当用户切换到此活动时,WebView的线程会继续运行!
有问题的线程是:
Thread [<17> WebViewCoreThread] (Running)
Thread [<25> CookieSyncManager] (Running)
Thread [<19> http0] (Running)
Thread [<29> http1] (Running)
Thread [<31> http2] (Running)
Thread [<33> http3] (Running)
Run Code Online (Sandbox Code Playgroud)
暂停这些线程中的每一个,并检查它正忙于做什么:
Thread [<17> WebViewCoreThread] (Suspended)
Object.wait(long, int) line: not available [native method]
MessageQueue(Object).wait() line: 288
MessageQueue.next() line: 148
Looper.loop() line: 110
WebViewCore$WebCoreThread.run() line: 471
Thread.run() line: 1060
Thread [<25> CookieSyncManager] (Suspended)
Object.wait(long, int) line: not available [native method]
MessageQueue(Object).wait(long) line: 326
MessageQueue.next() line: 144
Looper.loop() line: 110
CookieSyncManager(WebSyncManager).run() line: 90
Thread.run() line: 1060
Thread [<19> http0] (Suspended)
Object.wait(long, int) line: not available [native method]
RequestQueue(Object).wait() line: 288
ConnectionThread.run() line: 93
Run Code Online (Sandbox Code Playgroud)
我想知道如何告诉Looper每个线程退出.
我尝试调用webView.destroy()activity的onPause()方法,但它没有影响力.
当我禁用在webView(webView.loadUrl(...))中打开网页的调用时,这些线程自然不会启动,因此在离开活动后不会继续.
WebView离开他们的活动后,我怎么能让线程停止?
Sky*_*ymt 40
您应该能够通过调用webview上的onPause/onResume来停止/恢复这些线程.
然而,这些是隐藏的,所以你需要通过反思来做到这一点.以下代码对我有用:
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webView, (Object[]) null);
Run Code Online (Sandbox Code Playgroud)
其中webView是WebView的实例.
另请参阅:http://code.google.com/p/android/issues/detail?id = 10282
diy*_*ism 16
我的解决方案,在扩展的webview类中(在android 2.2中测试,andriod 2.3,android 3.1):
private boolean is_gone = false;
public void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
if (visibility == View.GONE) {
try {
WebView.class.getMethod("onPause").invoke(this); //stop flash
} catch (Exception e) {}
this.pauseTimers();
this.is_gone = true;
} else if (visibility == View.VISIBLE) {
try {
WebView.class.getMethod("onResume").invoke(this); //resume flash
} catch (Exception e) {}
this.resumeTimers();
this.is_gone = false;
}
}
public void onDetachedFromWindow() { //this will be trigger when back key pressed, not when home key pressed
if (this.is_gone) {
try {
this.destroy();
} catch (Exception e) {}
}
}
Run Code Online (Sandbox Code Playgroud)
Bol*_*ing 13
一个干净简单的解决方案,完成工作:
@Override
public void onPause() {
super.onPause();
webView.onPause();
webView.pauseTimers(); //careful with this! Pauses all layout, parsing, and JavaScript timers for all WebViews.
}
@Override
public void onResume() {
super.onResume();
webView.onResume();
webView.resumeTimers();
}
@Override
public void onDestroy() {
super.onDestroy();
parentViewGroup.removeAllViews(); //the viewgroup the webview is attached to
webView.loadUrl("about:blank");
webView.stopLoading();
webView.setWebChromeClient(null);
webView.setWebViewClient(null);
webView.destroy();
webView = null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38108 次 |
| 最近记录: |