2017-02-06 85 views
0

如果我在shouldOverrideUrlLoading上返回true,則加載新url時,web視圖無法處理onPageFinishAndroid webView無法處理pageFinish

@Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals("www.abc.com")) { 
       Intent intent = new Intent(context, MyActivity.class); 
       context.startActivity(intent); 
       return true; 
     } 
     return super.shouldOverrideUrlLoading(view, url); 
    } 

禁用頁面完成進度。

 @Override 
    public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      progress.setVisibility(View.GONE); 
    } 
+0

我解決了。它發生是因爲在MyActivity ** ondestroy()**方法中使用了** pauseTimers()**。 –

回答

0
try this code 

webview.setWebViewClient(new myWebClient()); 

     webview.setWebChromeClient(new WebChromeClient() 
     { 
      public void onProgressChanged(WebView view, int progressInt) 
      { 
       if (progressInt < 80 && progress.getVisibility() == ProgressBar.GONE) 
       { 
        progress.setVisibility(ProgressBar.VISIBLE); 
       } 

       if (progressInt >= 80) 
       { 
        progress.setVisibility(ProgressBar.GONE); 
       } 
      } 
     }); 

public class myWebClient extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
// TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
// TODO Auto-generated method stub 
      progress.setVisibility(View.VISIBLE); 
      view.loadUrl(url); 
      return true; 

     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
// TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
      progress.setVisibility(View.GONE); 
     } 
    } 
+0

感謝您的回答。 Bu我必須知道爲什麼我無法在shouldOverrideUrlLoading方法返回true後處理pageFinish。我想在不使用WebChromeClient的情況下解決此問題。 –

+0

@穆罕默德:「我必須知道爲什麼我無法在shouldOverrideUrlLoading方法返回true之後處理pageFinish。」因爲在shouldOverrideUrlLoading中返回true時,告訴當前webview不要加載url,這意味着也不會調用回調函數(onPageStarted,onPageFinished)。 https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,java.lang.String) –