当前位置:  开发笔记 > 编程语言 > 正文

WebView线程永不停止(WebViewCoreThread,CookieSyncManager,http [0-3])

如何解决《WebView线程永不停止(WebViewCoreThread,CookieSyncManager,http[0-3])》经验,为你挑选了3个好方法。

我使用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)

暂停这些线程中的每一个,并检查它正忙于做什么:

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

我想知道如何告诉Looper每个线程退出.

我尝试调用webView.destroy()activity的onPause()方法,但它没有影响力.
当我禁用在webView(webView.loadUrl(...))中打开网页的调用时,这些线程自然不会启动,因此在离开活动后不会继续.

WebView离开他们的活动后,我怎么能让线程停止?



1> Skymt..:

您应该能够通过调用webview上的onPause/onResume来停止/恢复这些线程.

然而,这些是隐藏的,所以你需要通过反思来做到这一点.以下代码对我有用:

Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webView, (Object[]) null);

其中webView是WebView的实例.

另请参阅:http://code.google.com/p/android/issues/detail?id = 10282


请注意,如果WebView具有某种运行的Javascript计时器,则这不起作用.

2> diyism..:

我的解决方案,在扩展的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) {}
     }
 }



3> Bolling..:

一个干净简单的解决方案,完成工作:

@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;
}


@GmanO removeAllViews()清除任何相关的视图,例如内置的缩放控件.如果在退出活动时显示它们,则会发生泄漏.请参阅/sf/ask/17360801/
推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有