onPageStart 被调用多次且 onPageFinished 未针对单页调用

发布于 2024-12-08 02:57:39 字数 1587 浏览 0 评论 0原文

我的问题与这个人的问题不同.. 我希望进度对话框在页面加载开始时启动,并在网页视图中页面加载完成时结束。我的问题是进度对话框启动并且永远不会被关闭。我设置了断点,它显示进度对话框启动并多次被关闭,然后它启动并且即使在页面加载完成后也不会被关闭。我的问题是为什么 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!

My question is different from this one guys..
I wany my progress dialog start when page load starts and end when the page load finished in my webview. My problem is the progress dialog starts and never get dismissed.I have set break points it shows that the progress dialog starts and get dismissed many times then it starts and not get dismissed even after page load completed. My question is why the onPageStarted getting executed many time for a single page loading?
and why onPageFinished not called after completion of page load?

       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();
        }

    });

My self tagged filtered Log is Like this for loading single page:

   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!

When I am clicking link on already loaded page it works fine. Here is
Log:

10-06 10:59:25.098: DEBUG/mytag(543): Page Loading Started
10-06 10:59:30.889: DEBUG/mytag(543): Page Loading Finished!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴情换悲伤 2024-12-15 02:57:39

使用 .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();
        }                
    }              

});

Check whether it is already open with .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();
        }                
    }              

});
柠檬色的秋千 2024-12-15 02:57:39

如果正在加载的页面中有 ajax 调用,则仅当这些调用完成时才会调用 onPageFinished()。
最好使用 window.setTimeout() 进行这些调用。在这种情况下,UI 线程不会在这些调用上被阻塞,并且 onPageFinished() 将在主页加载后立即调用。

If you have ajax calls in the page being loaded, onPageFinished() will be called only when those calls finish.
Better to make those calls with a window.setTimeout(). In that case, UI thread will not be blocked on those calls and onPageFinished() will be called as soon as the main page loads.

忆伤 2024-12-15 02:57:39

这是一个解决方案。我使用另一个 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);
        }
    }

});

Here is a solution. Instead of a loading dialog, i use another webview as splash-screen, but you can change it easily.

The trick is, to look if there is a new "onpagestart" right after the "onpagefinished". If this is the case, don't close the loading and wait for the next "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);
        }
    }

});
淑女气质 2024-12-15 02:57:39

有时,当初始页面加载时,某些脚本会进行重定向(因为它需要会话 ID,或者使页面再次加载的东西)。

您可以通过记录 url 参数来检查是否正在加载不同的页面(或带有附加参数的同一页面)。

我猜想,如果在第一页完成加载之前发出新的加载请求,则不会为该第一页调用 OnPageFinished 方法。

Sometimes happens that when the initial page loads, some script makes a redirect (because it needed a session ID, or something that would make the page load again).

You can check if a different page (or the same one with additional parameters) is loading by logging the url parameter.

I guess that if a new load request is made before the first page finishes loading, then the OnPageFinished method won't be called for that first page.

桜花祭 2024-12-15 02:57:39

当页面在 webview 应用程序上提交时: 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();
        }
    }

When a page submit on webview app: onPageStarted Fired, but onPageFinished not Fired so follow code below fixed and exactly

    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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文