|
| 1 | +package com.ai.listrelated.web; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Build; |
| 5 | +import android.webkit.WebSettings; |
| 6 | +import android.webkit.WebView; |
| 7 | + |
| 8 | +/** |
| 9 | + * <b>Project:</b> ListRelated <br> |
| 10 | + * <b>Create Date:</b> 2017/1/19 <br> |
| 11 | + * <b>Author:</b> qy <br> |
| 12 | + * <b>Address:</b> qingyongai@gmail.com <br> |
| 13 | + * <b>Description:</b> 通用的WebView的一些设置,方便直接使用以及扩展 <br> |
| 14 | + */ |
| 15 | +public class WebViewController { |
| 16 | + |
| 17 | + protected static final String TAG = WebViewController.class.getSimpleName(); |
| 18 | + |
| 19 | + protected Context mContext; |
| 20 | + protected WebView mWebView; |
| 21 | + protected WebSettings mSettings; |
| 22 | + protected MWebViewClient mWebViewClient; |
| 23 | + protected MWebChromeClient mWebChromeClient; |
| 24 | + |
| 25 | + public WebViewController(WebView webview) { |
| 26 | + mWebView = webview; |
| 27 | + mContext = mWebView.getContext(); |
| 28 | + mSettings = mWebView.getSettings(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 初始化WebView |
| 33 | + */ |
| 34 | + public void setupWebView() { |
| 35 | + mSettings.setSaveFormData(false); |
| 36 | + mSettings.setAllowFileAccess(true); |
| 37 | + mSettings.setDatabaseEnabled(true); |
| 38 | + mSettings.setJavaScriptEnabled(true); |
| 39 | + mSettings.setUseWideViewPort(true); |
| 40 | + mSettings.setAppCacheEnabled(true); |
| 41 | + mSettings.setDomStorageEnabled(true); |
| 42 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { |
| 43 | + mSettings.setDisplayZoomControls(false); |
| 44 | + } |
| 45 | + mSettings.setLoadWithOverviewMode(true); |
| 46 | + mSettings.setPluginState(WebSettings.PluginState.ON); |
| 47 | + // WebView自适应屏幕大小 |
| 48 | + mSettings.setDefaultTextEncodingName("UTF-8"); |
| 49 | + mSettings.setLoadsImagesAutomatically(true); |
| 50 | + // 设置可以支持缩放 |
| 51 | + mSettings.setSupportZoom(true); |
| 52 | + // 设置默认缩放方式尺寸是far |
| 53 | + mSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); |
| 54 | + // 设置出现缩放工具 |
| 55 | + mSettings.setBuiltInZoomControls(true); |
| 56 | + // 设置WebView的缓存模式 |
| 57 | + mSettings.setCacheMode(WebSettings.LOAD_DEFAULT); |
| 58 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 59 | + // 不然5.0以后http和https混合的页面会加载不出来 |
| 60 | + mSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 设置ua,需要注意的是我已经把原来的ua追加了,调用这个方法,只需要添加你想要添加的字符串即可 |
| 66 | + * |
| 67 | + * @param userAgent ua |
| 68 | + */ |
| 69 | + public void setUserAgent(String userAgent) { |
| 70 | + String origin = mSettings.getUserAgentString(); |
| 71 | + mSettings.setUserAgentString(origin + " " + userAgent); |
| 72 | + // KLog.i(TAG, "UA------------------>" + mSettings.getUserAgentString()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * 设置webViewClient |
| 77 | + * |
| 78 | + * @param webViewClient webViewClient必须继承{@link MWebViewClient} |
| 79 | + */ |
| 80 | + public void setWebViewClient(MWebViewClient webViewClient) { |
| 81 | + if (webViewClient != null) { |
| 82 | + mWebViewClient = webViewClient; |
| 83 | + mWebView.setWebViewClient(webViewClient); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 设置webChromeClient |
| 89 | + * |
| 90 | + * @param webChromeClient webChromeClient必须继承{@link MWebChromeClient} |
| 91 | + */ |
| 92 | + public void setWebChromeClient(MWebChromeClient webChromeClient) { |
| 93 | + if (webChromeClient != null) { |
| 94 | + mWebChromeClient = webChromeClient; |
| 95 | + mWebView.setWebChromeClient(webChromeClient); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 添加js调用java的接口 |
| 101 | + * |
| 102 | + * @param object |
| 103 | + * @param name |
| 104 | + */ |
| 105 | + @SuppressWarnings("All") |
| 106 | + public void addJavascriptInterface(Object object, String name) { |
| 107 | + mWebView.addJavascriptInterface(object, name); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments