我正在尝试在WebView中加载一些包含JavaScript的HTML代码。
现在,我想测试我的WebView是否在5次secondes之前加载。我尝试了getProgress()
方法,但有时我得到的结果是100,但我的Webview没有加载。
还有另一种方法可以确保我的Webview 100%加载吗?
这是我的代码的一部分:
sdk.getWebView().loadDataWithBaseURL("notreal/", data_html,MIME_TYPE,ENCODING_UTF_8,null);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run(){
Log.i("TAG", "progress fin = "+sdk.getWebView().getProgress());
if(sdk.getWebView().getProgress() <100){
//cancel the webView
sdk.getContext().runOnUiThread(new Runnable() {
@Override
public void run() {
sdk.getImgView().setVisibility(View.VISIBLE);
sdk.getWebView().setVisibility(View.GONE);
}
});
}
// else ,the Webview is loaded
else{
//prepare webview
sdk.getContext().runOnUiThread(new Runnable() {
@Override
public void run(){
// hide imageView
sdk.getImgView().setVisibility(View.GONE);
sdk.getWebView().setVisibility(View.VISIBLE);
}
});
}
}
};
timer.schedule(task, 5000);
答案 0 :(得分:79)
如上所述:How to listen for a WebView finishing loading a URL?
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
@Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
答案 1 :(得分:6)
您需要覆盖onPageFinished
WebViewClient
以下是您的示例
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//do what you want to do
}
}
答案 2 :(得分:3)
您可以尝试扩展WebChromeClient,覆盖onProgressChanged(WebView view, int newProgress)
并使用setWebChromeClient(WebChromeClient)
方法在WebView上注册它。这将使您的应用程序从您正在启动的其他线程中解放出来,以检查进度是否已更改。使用回调,它更简单。所以这将是第一件事。
另一个原因是我也在尝试这个进展状态,我得出了一些关于它的行为的结论:
stopLoading()
时,总而言之,此进度条指示WebView是否已完成加载网站,但就WebKit而言,不是完全下载页面。你必须记住,连接可能会崩溃,资源(图像,css,js)可能由于某种原因无法加载,JavaScript可以在页面完成加载等时加载更多资源。此进展无法告诉您网站是否有效内容是否已满载,它告诉您WebView认为这应该是全部。
关于如何检查页面是否已满载,我没有其他想法,我认为这是唯一的方法。
答案 3 :(得分:3)
检测页面是否已呈现的最佳方法是使用API中提供的onPageCommitVisible回调.onPageLoadFinished不合适,因为它过早交付(处理HTML时,但尚未呈现) )。
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageCommitVisible (WebView view,
String url)
}
}
答案 4 :(得分:2)
以上解决方案对我不起作用,我想确保我有100%的页面加载,然后注入javascript在页面上做一些深入的研究。我提出了以下解决方案,几乎没有偏离NeTeInStEiN提供的解决方案。这对每个人都不适用,但它对我有用。同样,这取决于您要从完成的页面中实现的目标。
String url; is from your activity which want to load this url
private class ExtendedWebViewClient extends WebViewClient {
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
public void onPageStarted(WebView paramWebView, String paramString,
Bitmap paramBitmap) {
super.onPageStarted(paramWebView, paramString, paramBitmap);
if(paramString.contentEquals(url)) {
webViewPreviousState = PAGE_STARTED;
} else {
webViewPreviousState = PAGE_REDIRECTED;
}
// DO YOU STUFF IF NEEDED
}
public void onPageFinished(WebView paramWebView, String paramString) {
if (webViewPreviousState == PAGE_STARTED) {
// I AM GETTING HERE WHEN MY PAGE IS LOADED 100%
// NOW ITS TIME FOR ME TO RUN JAVASCRIPT FUNCTIONS
// DO YOUR STUFF
}
}
希望这有帮助。
答案 5 :(得分:2)
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
//do your task
}
}
});
答案 6 :(得分:1)
为WebView设置WebChromeClient另一种确定何时完成页面加载的方法
mWebView.setWebChromeClient(new WebChromeClient(){
/*public void onProgressChanged (WebView view, int newProgress) Tell the host application the current progress of loading a page.
Parameters
view WebView: The WebView that initiated the callback.
newProgress int: Current page loading progress, represented by an
integer between 0 and 100.
*/
public void onProgressChanged(WebView view, int newProgress){
Toast.makeText(getActivity().getApplicationContext(),"Page loading : " + newProgress + "%",Toast.LENGTH_SHORT).show();
if(newProgress == 100){
// Page loading finish
Toast.makeText(getActivity().getApplicationContext(),"Page loaded",Toast.LENGTH_SHORT).show();
}
}
});
答案 7 :(得分:0)
以下代码完美无缺。页面成功加载后,将触发onPageFinished()
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Toast.makeText(getApplicationContext(),"Started",Toast.LENGTH_SHORT).show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Toast.makeText(getApplicationContext(),"Loaded",Toast.LENGTH_SHORT).show();
super.onPageFinished(view, url);
}
});
答案 8 :(得分:0)
我也有一个解决方案,因为已经经历了加载完成后需要显示 webview 的情况。希望大家理解。
webview.loadUrl(url)
webview.webViewClient = object : WebViewClient() {
override fun onPageCommitVisible(view: WebView?, url: String?) {
webview.visibility = View.GONE
// your method to call the css and load it into the existing webview
loadCSS()
super.onPageCommitVisible(view, url)
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (webview.progress == 100) {
webview.visibility = View.VISIBLE
}
progressBar.visibility = View.GONE
}
}