2016-04-01 73 views
2

在我的應用程序中,我使用webview打開一個url。網址非常簡單,只需要幾秒就可以加載。但是當url完全加載時,進度條不會隱藏。我無法弄清楚什麼是錯的。請幫幫我。下面我發佈代碼。Android:進度條加載url,繼續加載

的WebView類

public class noti_cse extends BaseActivity { 
    private WebView webView; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.noti_cse); 
     // overridePendingTransition(0, 0); 

     // webView = (WebView) findViewById(R.id.webcse); 
     // webView.setWebViewClient(new MyWebViewClient()); 
     if (!DetectConnection.checkInternetConnection(this)) { 
      Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show(); 
     } else { 
      webView = (WebView) findViewById(R.id.webcse); 
      webView.setWebViewClient(new CustomWebViewClient()); 
      webView.clearCache(true); 
      webView.clearHistory(); 
      webView.getSettings().setJavaScriptEnabled(true); 
      webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
      webView.getSettings().setBuiltInZoomControls(true); 
      startWebView("http://notify.php"); 
     } 


     // webView.getSettings().setJavaScriptEnabled(true); 
     // startWebView("http://notify.php"); 
     // webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 

     // webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 


    } 


    private void startWebView(String url) { 

     //Create new webview Client to show progress dialog 
     //When opening a url or click on link 

     webView.setWebViewClient(new WebViewClient() { 
      ProgressDialog progressDialog; 

      //If you will not use this method url links are opeen in new brower not in webview 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       return true; 
      } 

      //Show loader on url load 
      public void onLoadResource(WebView view, String url) { 
       if (progressDialog == null) { 
        // in standard case YourActivity.this 
        progressDialog = new ProgressDialog(noti_cse.this); 
        progressDialog.setMessage("On the Way!!!"); 
        progressDialog.setCanceledOnTouchOutside(false); 
        progressDialog.show(); 
       } 
      } 

      public void onPageFinished(WebView view, String url) { 
       try { 
        if (progressDialog.isShowing()) { 
         progressDialog.dismiss(); 
         progressDialog = null; 
        } 
       } catch (Exception exception) { 
        exception.printStackTrace(); 
       } 
      } 

     }); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl(url); 

    } 

    private class CustomWebViewClient extends WebViewClient { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (!DetectConnection.checkInternetConnection(noti_cse.this)) { 
       Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show(); 
      } else { 
       view.loadUrl(url); 
      } 
      return true; 
     } 
    } 

    @Override 
    protected boolean useDrawerToggle() { 
     return false; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == R.id.action_placements) 
      return true; 

     if (item.getItemId() == android.R.id.home) 
      onBackPressed(); 

     return super.onOptionsItemSelected(item); 
    } 

回答

0

要設置2 WebViewClient。

第一:

webView.setWebViewClient(new CustomWebViewClient()); 

在這之後,你在呼喚

startWebView() 

這臺新WebviewClient。

你可以改變你的方法如下圖所示:

private void startWebView(String url) { 

    //Create new webview Client to show progress dialog 
    //When opening a url or click on link 

    webView.setWebViewClient(new WebViewClient() { 
     ProgressDialog progressDialog; 

     //If you will not use this method url links are opeen in new brower not in webview 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (!DetectConnection.checkInternetConnection(noti_cse.this)) { 
      Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show(); 
      } else { 
       view.loadUrl(url); 
      } 
      return true; 
     } 

     //Show loader on url load 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon){ 
      if (progressDialog == null) { 
       // in standard case YourActivity.this 
       progressDialog = new ProgressDialog(noti_cse.this); 
       progressDialog.setMessage("On the Way!!!"); 
       progressDialog.setCanceledOnTouchOutside(false); 
       progressDialog.show(); 
      } 
      super.onPageStarted(view, url, favicon); 
     } 

     public void onPageFinished(WebView view, String url) { 
      try { 
       if (progressDialog.isShowing()) { 
        progressDialog.dismiss(); 
        progressDialog = null; 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
     } 

    }); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl(url); 

} 

,並刪除這條線的形式onCreate()方法:

webView.setWebViewClient(new CustomWebViewClient());