天天看點

WebView加載URL配置

加載過程中遇到問題:網頁内部點選失效,網頁加載不完整。

記錄一下成功後的webView的配置

private void showWebView(String url) {
        webView = dataBinding.webView;
        WebSettings webSettings = webView.getSettings();
//      if //(!webView.getSettings().getUserAgentString().contains(NetworkUtils.AGENT_TAG)) {
//          webSettings.setUserAgentString(webSettings.getUserAgentString() + //NetworkUtils.AGENT_TAG);
//      }
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize( *  * );
        String appCachePath = view.getContext().getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            webSettings.setDisplayZoomControls(false);
            webSettings.setSupportZoom(true);
            webSettings.setBuiltInZoomControls(true);
        }
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http") || url.startsWith("https")) {
                    view.load(url);
                    return true;
                } else {
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        view.getContext().startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        });
        }
           
如果需要無網緩存則加入這兩句代碼  總的來說就是無網使用下面的模式 有網使用DEFAULT模式
           
if(isNetworkAvaliable(this)) {
            webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);  //設定 緩存模式
        }else{
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //設定 緩存模式
        }
           

繼續閱讀