Android WebView TimeOut

后端 未结 2 1426
故里飘歌
故里飘歌 2020-12-03 14:23

Is there a way to set the timeout value in WebView? I want the WebView to be timeouted if the url is too slow to response.

相关标签:
2条回答
  • 2020-12-03 14:37

    You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.

    0 讨论(0)
  • 2020-12-03 14:54

    We can use onLoadResource method of WebViewClient instead of Timer. Like this:

    webView.setWebViewClient(new WebViewClient() {
    
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    
        @Override 
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.show();
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d("WEBCLIENT", "onPageFinished");
        }
    
        @Override 
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            Log.d("WEBCLIENT","onLoadResource");
    
            if(webView.getProgress() == 100) {
                progressDialog.dismiss();
            }    
        }
    }
    
    0 讨论(0)
提交回复
热议问题