WebView相关设置

2018-08-07  本文已影响0人  ComeAsExpected
  1. 重定向问题

通过setWebViewClient方法重写WebViewClientEx内部的shouldOverrideUrlLoading(WebView view, String url)方法控制页面跳转。

WebView.HitTestResult hitTestResult = view.getHitTestResult();
通过hitTestResult.getType()判断重定向类型,类型如下;
网页首次进入自动重定向 getType = 0

/**
 * Default HitTestResult, where the target is unknown.
 */
public static final int UNKNOWN_TYPE = 0;
/**
 * @deprecated This type is no longer used.
 */
@Deprecated
public static final int ANCHOR_TYPE = 1;
/**
 * HitTestResult for hitting a phone number.
 */
public static final int PHONE_TYPE = 2;
/**
 * HitTestResult for hitting a map address.
 */
public static final int GEO_TYPE = 3;
/**
 * HitTestResult for hitting an email address.
 */
public static final int EMAIL_TYPE = 4;
/**
 * HitTestResult for hitting an HTML::img tag.
 */
public static final int IMAGE_TYPE = 5;
/**
 * @deprecated This type is no longer used.
 */
@Deprecated
public static final int IMAGE_ANCHOR_TYPE = 6;
/**
 * HitTestResult for hitting a HTML::a tag with src=http.
*/
public static final int SRC_ANCHOR_TYPE = 7;
/**
 * HitTestResult for hitting a HTML::a tag with src=http +  HTML::img.
 */
public static final int SRC_IMAGE_ANCHOR_TYPE = 8;
/**
 * HitTestResult for hitting an edit text area.
 */
public static final int EDIT_TEXT_TYPE = 9;

2。 webview常见的相关设置

// 调试WebView
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (0 != (BaseApplication.getInstance().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
}

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(false);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setSavePassword(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setUseWideViewPort(true);
// 限制正文字体大小为100%,防止修改系统字体后造成的正文布局错乱
webView.getSettings().setTextZoom(100); 
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
//无网时取本地缓存
if (!NetUtils.checkNetwork()) {
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
} else {
    // load online by default
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
}
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setGeolocationEnabled(true);        webView.getSettings().setGeolocationDatabasePath(webView.getContext().getCacheDir().toString());       webView.getSettings().setUserAgentString(NetUtils.getUserAgent(true));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

WebChromeClient webChromeClient = new WebChromeClientEx(webView) {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
                
    }

    @Override
    public void onHideCustomView() {
                
    }
};
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(new WebViewClientEx(webView) {

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        return super.shouldInterceptRequest(view, url);
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return super.shouldInterceptRequest(view, request);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view,url);
    }

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

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

    @Override
    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        super.onReceivedHttpAuthRequest(view, handler, host, realm);
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse){
        super.onReceivedHttpError(view, request, errorResponse);
    }
    
    @TargetApi(android.os.Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
});
上一篇下一篇

猜你喜欢

热点阅读