Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - WebView threads never stop (WebViewCoreThread, CookieSyncManager, http[0-3])

I use a WebView to display some internet content on one of our app's Activities.
The problem is that when the user switches out of this activity, WebView's threads keep running!
The problematic threads are:

Thread [<17> WebViewCoreThread] (Running)
Thread [<25> CookieSyncManager] (Running)
Thread [<19> http0] (Running)
Thread [<29> http1] (Running)
Thread [<31> http2] (Running)
Thread [<33> http3] (Running)

Pausing each one of these threads, and checking what it is busy doing:

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

I wonder how can I tell the Looper in each of those threads to quit.

I tried calling webView.destroy() in the activity's onPause() method, but it had no influence.
When I disable the call for opening a web page in the webView ( webView.loadUrl(...) ), those threads naturally are not started, and therefore don't stay on after leaving the activity.

Any ideas as to how I can make WebView's threads stop after leaving their activity?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You should be able to stop / resume these threads by calling the onPause / onResume on the webview.

Those are however hidden, so you will need to do it through reflection. The following code worked for me:

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

Where webView is the instance of WebView.

Also see: http://code.google.com/p/android/issues/detail?id=10282


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...