2015-10-07 98 views
0

在WebView中測試一個url意識到它需要大約6〜8秒的加載時間。與iOS相比,WebView只需要2秒鐘。這兩款設備都在同一個WiFi網絡上。我在WebView中使用以下設置。WebView緩慢加載url

//Manifest 
//android:hardwareAccelerated="true" 

webView = (WebView) findViewById(R.id.wvProduct); 

      final WebSettings settings = webView.getSettings(); 

      settings.setJavaScriptEnabled(true); 
      settings.setAppCacheEnabled(true); 
      settings.setDomStorageEnabled(true); 
      settings.setLoadsImagesAutomatically(true); 
      settings.setDatabaseEnabled(true); 
      settings.setRenderPriority(WebSettings.RenderPriority.HIGH); 
      settings.setCacheMode(WebSettings.LOAD_NO_CACHE); 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); 
      } 

      //webView.setWebViewClient(new MyWebClient()); 
      webView.loadUrl(url); 

if (Build.VERSION.SDK_INT >= 19) { 

      webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 

     } else { 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 

       webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

      } 

     } 

public class MyWebClient extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, 
               String urlNewString) { 
      view.loadUrl(urlNewString); 
      return true; 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      if(LoadingDialog.instanceProgressDialog().isShowing()) { 
       LoadingDialog.dismissDialog(); 
      } 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, 
            String description, String failingUrl) { 
      super.onReceivedError(view, errorCode, description, failingUrl); 

      if(LoadingDialog.instanceProgressDialog() != null) { 

       if (LoadingDialog.instanceProgressDialog().isShowing()) { 

        LoadingDialog.dismissDialog(); 

       } 

      } 

      Log.e(Constants.ERROR, 
        "****Error: " + description + " code: " + errorCode); 
     } 

    } 

誰刪除了用於去除裝載機對話框,並贏了下來2個secounds(4〜5)裝載WebViewClient陌生人。如何提高WebView加載時間,此URL是一個移動商務,包含圖像和JavaScript。這個問題主要發生在第一次充電時(即惡化),即離開活動並返回最佳時間短(3〜4秒)。

回答

0

問題是這條線。

webView.setWebViewClient(new WebViewClient()); 

奇怪的是添加一個客戶端到web視圖的加載時間增加。當我刪除這行時間下降了2秒。

+0

一般我猜android web視圖是該死的慢 –