2014-03-31 100 views
4

如果加載顯示錯誤消息需要很長時間,我想超時查看webview。我正在使用setWebViewClient,因爲我需要使用public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)如果加載時間超過一定時間,Android超時webview

我一直在環顧四周,看到我可以使用方法onProgressChanged(WebView view, int newProgress)。現在我不能在setWebViewClient中使用這種方法,也無法弄清楚如何解決這個問題。我遇到的另一個問題是一旦頁面加載後進度條永不消失,我也無法向方法public void onPageFinished(WebView view, String url)添加斷點。

Web視圖設置方法:

public void WebViewSettings(){ 

    webView = (WebView) findViewById(R.id.webview); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.getSettings().setSupportZoom(true); 
    webView.getSettings().setLoadWithOverviewMode(true); 
    webView.canGoBack(); 

    webView.setWebViewClient(new WebViewClient(){ 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Uri.parse(url).getHost().equals(urlString)) { 
       // This is my web site, so do not override; let my WebView load the page 
       return false; 
      } 
      // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
      startActivity(intent); 
      return true; 
     } 

     @Override 
     public void onLoadResource(WebView view, String url) { 
      // Check to see if there is a progress dialog 
      if (progressDialog == null) { 
       progressDialog = new ProgressDialog(context); 
       progressDialog.setTitle("Loading..."); 
       progressDialog.setMessage("Please wait."); 
       //progressDialog.setCancelable(false); 
       progressDialog.setIndeterminate(true); 
       progressDialog.show(); 
      } 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // Page is done loading; 
      // hide the progress dialog and show the webview 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
       progressDialog = null; 
       webView.setEnabled(true); 
      } 
     } 

     @Override 
     public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { 
      handler.proceed(); 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
      Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

所以問題我已經是一次網頁加載進度條沒有得到清除,我需要超時web視圖,如果它接管一定的時間加載。它看起來像進度條顯示,然後消失就像它應該,然後再次開始加載,並不會停止。謝謝

回答

8

我已更新您的webview設置方法,請在下面找到更新的代碼。 我添加了一個loaderTimerTask類。添加了計時器的代碼併爲更多理解添加了評論。如果您需要,請更新此代碼。

private boolean isPageLoadedComplete = false; //declare at class level 

public void WebViewSettings(){ 

webView = (WebView) findViewById(R.id.webview); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setBuiltInZoomControls(true); 
webView.getSettings().setSupportZoom(true); 
webView.getSettings().setLoadWithOverviewMode(true); 
webView.canGoBack(); 

/** 
    *I had put code here 
    */ 
Timer myTimer = new Timer(); 
    //Start this timer when you create you task 
myTimer.schedule(loaderTask, 3000); // 3000 is delay in millies 

webView.setWebViewClient(new WebViewClient(){ 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals(urlString)) { 
      // This is my web site, so do not override; let my WebView load the page 
      return false; 
     } 
     // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(intent); 
     return true; 
    } 

    @Override 
    public void onLoadResource(WebView view, String url) { 
     // Check to see if there is a progress dialog 
     if (progressDialog == null) { 
      progressDialog = new ProgressDialog(context); 
      progressDialog.setTitle("Loading..."); 
      progressDialog.setMessage("Please wait."); 
      //progressDialog.setCancelable(false); 
      progressDialog.setIndeterminate(true); 
      progressDialog.show(); 
     } 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
      isPageLoadedComplete = true; 
     // Page is done loading; 
     // hide the progress dialog and show the webview 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      progressDialog = null; 
      webView.setEnabled(true); 
     } 
    } 

    @Override 
    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { 
     handler.proceed(); 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
     Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
    } 
}); 
} 

/** 
    *This class is invoke when you times up. 
    */ 
class loaderTask extends TimerTask { 
    public void run() { 
    System.out.println("Times Up"); 
    if(isPageLoadedComplete){ 
    }else{ 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      progressDialog = null; 
      webView.setEnabled(true); 
     } 
     //show error message as per you need. 
    } 
    } 
} 
+0

謝謝你的伴侶。唯一的一點是,當我添加一個錯誤消息,你已經把'//顯示錯誤消息,根據你的需要。',我得到一個運行時錯誤,說'不能創建處理程序內線程沒有調用looper。製備()'。現在我已經環顧四周,顯然是與它的線程有關,你可以提出任何建議嗎? – Paddy1990

+1

我已經設法解決它我不得不明確告訴它運行'runOnUiThread(新的Runnable(){公共無效運行(){Toast.makeText(上下文,「網頁超時,再試一次」 ,Toast.LENGTH_SHORT).show();}});' – Paddy1990