我的问题与this一个人有所不同.. 我在页面加载开始时开始我的进度对话框,在网页加载完成页面加载时结束。我的问题是进度对话框开始并且永远不会被解雇。我设置了断点,它显示进度对话框开始并被多次解散然后它开始并且即使在页面加载完成后也不会被解雇。我的问题是为什么onPageStarted多次执行单页加载? 以及为什么在完成页面加载后没有调用onPageFinished?
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myWebView.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(myWebView, url, favicon);
Log.d("mytag","Page Loading Started");
//myURLProgressDialog= ProgressDialog.show(WebviewExampleActivity.this, "Page Loading", "Wait for a moment...");
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("mytag","Page Loading Finished!");
super.onPageFinished(myWebView, url);
//myURLProgressDialog.dismiss();
}
});
我自我标记的过滤日志就像这样加载单页:
10-06 10:32:49.298: DEBUG/mytag(508): Page Loading Started
10-06 10:32:49.998: DEBUG/mytag(508): Page Loading Started
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Finished!
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Started
10-06 10:33:00.898: DEBUG/mytag(508): Page Loading Finished!
当我点击已加载页面上的链接时,它可以正常工作。这是 经度:
10-06 10:59:25.098: DEBUG/mytag(543): Page Loading Started
10-06 10:59:30.889: DEBUG/mytag(543): Page Loading Finished!
答案 0 :(得分:6)
检查.isShowing()
是否已打开。
final ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
browser.setWebViewClient(new myViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!mProgressDialog.isShowing())
{
mProgressDialog.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
});
答案 1 :(得分:5)
如果您正在加载的页面中有ajax调用,则只有在这些调用完成时才会调用onPageFinished()。 最好使用window.setTimeout()进行调用。在这种情况下,这些调用不会阻止UI线程,并且主页加载后将立即调用onPageFinished()。
答案 2 :(得分:0)
有时会发生在初始页面加载时,某些脚本会进行重定向(因为它需要会话ID,或者会使页面再次加载的内容)。
您可以通过记录url参数来检查是否正在加载不同的页面(或带有其他参数的页面)。
我想如果在第一页完成加载之前发出新的加载请求,则不会为第一页调用OnPageFinished方法。
答案 3 :(得分:0)
这是一个解决方案。我使用另一个webview作为启动画面,而不是加载对话框,但您可以轻松更改它。
诀窍是,在“onpagefinished”之后查看是否有新的“onpagestart”。如果是这种情况,请不要关闭加载并等待下一个“onpagefinished”。
myWebView.setWebViewClient(new WebViewClient() {
boolean loadingFinished = true;
boolean redirect = false;
long last_page_start;
long now;
// Load the url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("p","pagestart");
loadingFinished = false;
last_page_start = System.nanoTime();
show_splash();
}
// When finish loading page
public void onPageFinished(WebView view, String url) {
Log.i("p","pagefinish");
if(!redirect){
loadingFinished = true;
}
//call remove_splash in 500 miSec
if(loadingFinished && !redirect){
now = System.nanoTime();
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
remove_splash();
}
},
500);
} else{
redirect = false;
}
}
private void show_splash() {
if(myWebView.getVisibility() == View.VISIBLE) {
myWebView.setVisibility(View.GONE);
myWebView_splash.setVisibility(View.VISIBLE);
}
}
//if a new "page start" was fired dont remove splash screen
private void remove_splash() {
if (last_page_start < now) {
myWebView.setVisibility(View.VISIBLE);
myWebView_splash.setVisibility(View.GONE);
}
}
});
答案 4 :(得分:0)
当在webview app上提交页面时:onPageStarted已被触发,但是onPageFinished未被触发,请按照下面的代码修复并准确
ProgressDialog pd = null;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(pd == null || !pd.isShowing()) {
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Please wait");
pd.setMessage("App is loading...");
pd.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pd != null) {
pd.dismiss();
}
}