在WebView android中缓慢加载URL

时间:2018-03-26 09:09:14

标签: java android webview android-webview

我在应用程序中使用WebView加载网站。

在webview中加载我的网站的时间过长,但在Chrome浏览器应用程序中的速度非常快。

如何在WebView中加快时间加载网站?

我可以使用chrome而不是WebView android吗?或在WebView中使用chrome core?

我使用此代码但不起作用:

webView.setWebChromeClient(new WebChromeClient());

我的代码java webiew:

final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);

    // webview settings
    webView.getSettings().setBuiltInZoomControls(false);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

    webView.setBackgroundColor(getResources().getColor(R.color.global_bg_front));
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // fixes scrollbar on Froyo
    class MyJavaScriptInterface
    {
        @JavascriptInterface
        @SuppressWarnings("unused")
        public void processHTML(String html)
        {
            Log.e("classclass:",html);
            if (html.contains("class")){
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl(url2);
                    }
                });
            }else {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        showContent();
                    }
                });
            }
        }
    }
    webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new WebViewClient()
    {

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

            runTaskCallback(new Runnable()
            {
                public void run()
                {
                    timeout = false;
                    Log.e("onPageFinished::",url);
                    if(getActivity()!=null && mSuccess)
                    {
                        if(mState.equals("geo")&&isFirst) {
                            isFirst=false;
                            webView.loadUrl("javascript:SendData("+Lat+","+Lon+")");
                        }
                        else if (mState.equals("state")&&isFirst2){
                            isFirst2=false;
                            webView.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementById('select2-w0-container').innerHTML);");
                        }else if (mState.equals("geo")){
                            coUrl=url;
                            mState="normallgeo";
                          //  webView.clearHistory();
                            showContent();
                        }else if (mState.equals("state")){
                            coUrl=url;
                            mState="normallstate";
                            //webView.clearHistory();
                            showContent();
                        }
                        else{
                           showContent();
                        }
                    }
                }
            });
        }


        @Override
        public void onReceivedError(final WebView view, final int errorCode, final String description, final String failingUrl)
        {
            runTaskCallback(new Runnable()
            {
                public void run()
                {
                    timeout = false;
                    if(getActivity()!=null)
                    {
                        Log.e("failingUrl::",failingUrl);
                        Log.e("onReceivedError::",description);
                        erUrl=failingUrl;
                        showError();
                        mSuccess = false;
                    }
                }
            });
        }
        public void onPageStarted(WebView view, final String url, Bitmap favicon)
        {
            // TODO show you progress image
            super.onPageStarted(view, url, favicon);
            if (url.equals("https://chindi.ir/")|url.equals("https://chindi.ir")|url.equals("https://www.chindi.ir/")|url.equals("https://www.chindi.ir")|url.equals("http://chindi.ir/")|url.equals("http://chindi.ir")) {
                getActivity().finish();
            }else {
               showProgress();
            }
            Runnable run = new Runnable() {
                public void run() {
                    if(timeout) {
                        // do what you want
                        if(mViewState==ViewState.PROGRESS) {
                            erUrl = mUrl;
                            showError();
                            mSuccess = false;
                        }
                    }
                }
            };
            Handler myHandler = new Handler(Looper.myLooper());
            myHandler.postDelayed(run, 30000);
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
             if(url!=null && (url.startsWith("http://") || url.startsWith("https://")))
            {
                    return false;
            }
            else if(url!=null && url.startsWith("mailto:"))
            {
                MailTo mailTo = MailTo.parse(url);
                startEmailActivity(mailTo.getTo(), mailTo.getSubject(), mailTo.getBody());
                return true;
            }
            else if(url!=null && url.startsWith("tel:"))
            {
                startCallActivity(url);
                return true;
            }
            else if(url!=null && url.startsWith("sms:"))
            {
                startSmsActivity(url);
                return true;
            }
            else if(url!=null && url.startsWith("geo:"))
            {
                startMapSearchActivity(url);
                return true;
            }
            else
            {
                return false;
            }
        }
    });
    webView.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
            {
                WebView webView = (WebView) v;

                switch(keyCode)
                {
                    case KeyEvent.KEYCODE_BACK:

                        if(webView.canGoBack())
                        {
                            webView.goBack();
                            return true;
                        }
                        break;
                }
            }

            return false;
        }
    });
    webView.requestFocus(View.FOCUS_DOWN); // http://android24hours.blogspot.cz/2011/12/android-soft-keyboard-not-showing-on.html
    webView.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if(!v.hasFocus())
                    {
                        v.requestFocus();
                    }
                    break;
            }

            return false;
        }
    });

0 个答案:

没有答案