WebView缓慢加载网址

时间:2015-10-07 01:18:47

标签: android html ios webview

在WebView中测试网址意识到加载大约需要6~8秒。与iOS相比,WebView只需2秒。两个设备都在同一个wifi网络上。我在WebView中使用以下设置。

//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);
        }

    }

陌生人删除了用于移除加载程序对话框的WebViewClient,并且加载了2秒(4~5)。如何改善WebView加载时间,这个url是一个移动商务,包含图像和javascript。这个问题在第一次充电时主要发生(恶化),即离开活动并返回最佳时间很短(3~4秒)。

1 个答案:

答案 0 :(得分:0)

问题在于这一行。

webView.setWebViewClient(new WebViewClient());

奇怪的是,将一个客户端添加到webview加载时间增加。当我删除此行时间下降了2秒。