stopLoading() 究竟做了啥?
Posted
技术标签:
【中文标题】stopLoading() 究竟做了啥?【英文标题】:What does stopLoading() really do?stopLoading() 究竟做了什么? 【发布时间】:2012-07-23 01:34:23 【问题描述】:是的,我知道 stopLoading() 的文档说“停止当前负载。”
但是当我尝试使用它在加载新页面之前停止加载当前正在进行的页面时,它似乎没有表现as desired:
07-24 12:53:30.177: V/WebView.loadUrl: http://www.google.com
07-24 12:53:30.227: V/WebViewClient.onPageStarted: http://www.google.com
===> WebView.stopLoading() called here <====
07-24 12:53:31.917: V/WebView.loadUrl: http://www.***.com
07-24 12:53:32.697: V/WebViewClient.onPageFinished: http://www.google.com
07-24 12:53:32.767: V/WebViewClient.onPageStarted: http://www.***.com
07-24 12:53:33.587: V/WebViewClient.onPageFinished: http://www.***.com
正如您在日志中看到的那样,第一个 loadUrl()
的 WebViewClient.onPageFinished()
被调用,尽管 WebView.stopLoading()
被调用大约 1 秒前。
这是为什么呢?
stopLoading() 的真正作用是什么?
【问题讨论】:
我猜它只是设置了一个标志来告诉 WebView 不要运行任何钩子,如 onPageFinished 等。我认为它实际上不会停止网络连接。 【参考方案1】:you786 是对的:
public void stopLoading()
checkThread();
// TODO: should we clear all the messages in the queue before sending
// STOP_LOADING?
switchOutDrawHistory();
mWebViewCore.sendMessage(EventHub.STOP_LOADING);
对于他们在剩余消息中标记的 TODO,将首先得到处理。所以这导致了我只是好奇:当停止加载时
WebViewClient.onReceivedError(WebView view, int errorCode, String description, String failingUrl)
WebView 并没有真正停止,而是加载自己的 404 页面(如果您不调用 stopLoading(),它通常会执行此操作):
11-07 16:30:01.112: I/MainActivity(19189): Loading: http://92.53.45.42
11-07 16:30:01.253: V/MainActivity.WebViewClient(19189): SHOULD_INTERCEPT_REQUEST: http://92.53.45.42/
11-07 16:30:01.347: V/MainActivity.WebViewClient(19189): PAGE_STARTED: http://92.53.45.42/
11-07 16:30:01.347: V/MainActivity.WebChromeClient(19189): ON_PROGRESS_CHANGED: 10
11-07 16:30:01.347: V/MainActivity.WebViewClient(19189): LOAD_RESOURCE: http://92.53.45.42/
11-07 16:30:25.292: I/GATE(19189): <GATE-M>DEV_ACTION_ERROR</GATE-M>
11-07 16:30:25.300: E/MainActivity.WebViewClient(19189): (CONNECT - Failed to connect to the server) -> http://92.53.45.42/
11-07 16:30:25.300: W/MainActivity.WebViewClient(19189): loading stopped..:
11-07 16:30:25.300: V/MainActivity.WebViewClient(19189): PAGE_STARTED: http://92.53.45.42/
11-07 16:30:25.300: I/MainActivity.WebViewClient(19189): PAGE_FINISHED: http://92.53.45.42/
11-07 16:30:25.339: I/MainActivity.WebChromeClient(19189): ON_RECEIVED_TITLE: Webseite nicht verfügbar
11-07 16:30:25.339: V/MainActivity.WebChromeClient(19189): GET_VISITED_HISTORY
11-07 16:30:25.339: I/GATE(19189): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
11-07 16:30:25.339: V/MainActivity.WebChromeClient(19189): ON_PROGRESS_CHANGED: 100
11-07 16:30:25.339: I/MainActivity.WebViewClient(19189): PAGE_FINISHED: http://92.53.45.42/
【讨论】:
以上是关于stopLoading() 究竟做了啥?的主要内容,如果未能解决你的问题,请参考以下文章
setUseWideViewPort() 和 setLoadWithOverviewMode() 究竟做了啥?