com.tencent.smtt.sdk.WebSettings Java Examples
The following examples show how to use
com.tencent.smtt.sdk.WebSettings.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EBrowserSetting.java From appcan-android with GNU Lesser General Public License v3.0 | 6 votes |
@SuppressWarnings("rawtypes") private void invokeHtml5(WebSettings setting) { try { String path = mBrwView.getContext().getDir("database", 0).getPath(); Class[] paramTypes = new Class[]{boolean.class}; Class[] param1 = new Class[]{String.class}; Method databaseEnabled = WebSettings.class.getMethod("setDatabaseEnabled", paramTypes); databaseEnabled.invoke(setting, true); Method domStorageEnabled = WebSettings.class.getMethod("setDomStorageEnabled", paramTypes); domStorageEnabled.invoke(setting, true); Method databasePath = WebSettings.class.getMethod("setDatabasePath", param1); databasePath.invoke(setting, path); } catch (Exception e) { ; } }
Example #2
Source File: X5WebView.java From TBSVideoPlay with Apache License 2.0 | 6 votes |
/** * 初始化webview设置 */ private void initWebViewSettings() { WebSettings webSetting = this.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true); // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); }
Example #3
Source File: X5WebView.java From polyvideo with Apache License 2.0 | 6 votes |
private void initWebViewSettings() { WebSettings webSetting = this.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true); // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension // settings 的设计 }
Example #4
Source File: WrapperWebView.java From AndroidHybridLib with Apache License 2.0 | 6 votes |
private void initWebViewSettings() { WebSettings ws = mWebView.getSettings(); ws.setDefaultTextEncodingName("utf-8"); ws.setJavaScriptEnabled(true); ws.setPluginsEnabled(true); ws.setDomStorageEnabled(true); ws.setRenderPriority(com.tencent.smtt.sdk.WebSettings.RenderPriority.HIGH); ws.setAllowFileAccess(true); ws.setAllowContentAccess(true); ws.setAppCacheEnabled(false); ws.setCacheMode(com.tencent.smtt.sdk.WebSettings.LOAD_NO_CACHE); ws.setSaveFormData(true); ws.setJavaScriptCanOpenWindowsAutomatically(true); ws.setLoadsImagesAutomatically(true); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { ws.setAllowFileAccessFromFileURLs(true); ws.setAllowUniversalAccessFromFileURLs(true); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mWebView.setWebContentsDebuggingEnabled(true); } }
Example #5
Source File: X5WebView.java From Readhub with Apache License 2.0 | 6 votes |
private void initWebViewSettings() { WebSettings webSetting = this.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(false); webSetting.setBuiltInZoomControls(false); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); webSetting.setAppCacheEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); }
Example #6
Source File: X5WebView.java From FastAndroid with Apache License 2.0 | 6 votes |
@SuppressLint("SetJavaScriptEnabled") private void initWebViewSettings() { WebSettings webSetting = this.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true); // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension // settings 的设计 }
Example #7
Source File: X5WebView.java From CacheWebView with MIT License | 6 votes |
private void initWebViewSettings() { WebSettings webSetting = this.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true); // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension // settings 的设计 }
Example #8
Source File: MyTestActivity.java From AndroidFrame with Apache License 2.0 | 5 votes |
private void initWebview() { InputStream fs = this.getResources().openRawResource( R.raw.jsbridge_init); jsbridgeInitScript = Util.getLocalText(fs); mWebview.setWebViewClient(new MyWebViewClient()); mWebview.setWebChromeClient(new MyWebChromeClient()); WebSettings webSettings = mWebview.getSettings(); try { // setPageCacheCapacity(webSettings); webSettings.setSavePassword(false);//是否保存密码,api18已经过时 webSettings.setSaveFormData(false);//是否保存表单数据 webSettings.setJavaScriptEnabled(true);//允许JavaScript运行 webSettings.setTextZoom(100);//设置网页文本缩放百分比,默认100 webSettings.setDomStorageEnabled(true);//是否允许网页向本地存储数据 webSettings.setAppCacheEnabled(true);//这是允许缓存,包括session数据和持久化数据 String path = Environment.getExternalStorageDirectory() + "jsTest"; webSettings.setAppCachePath(path);//设置缓存路径,与setAppCacheEnabled配合使用 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//先从网络获取,如果网络不可用则从本地获取 String userAgent = webSettings.getUserAgentString(); webSettings.setUserAgentString(userAgent);//可以自定义用户代理 } catch (Exception e) { e.printStackTrace(); } WebViewJavascriptBridge ylBridge = new WebViewJavascriptBridge(this, mHandler, mWebview); mWebview.addJavascriptInterface(ylBridge, "AndroidWebViewJavascriptBridge"); // if (android.os.Build.VERSION.SDK_INT <= 18) { mWebview.loadUrl("file:///android_asset/jstest.html"); // mWebview.loadUrl("https://www.baidu.com/"); // } else { // mWebview.evaluateJavascript("https://www.baidu.com/", null); // } }
Example #9
Source File: WebViewFragment.java From Dainty with Apache License 2.0 | 5 votes |
@SuppressLint("SetJavaScriptEnabled") private void setSettings(WebSettings setting) { //noinspection deprecation setting.setJavaScriptEnabled(true); setting.setJavaScriptCanOpenWindowsAutomatically(true); setting.setAllowFileAccess(true); setting.setSupportZoom(true); setting.setBuiltInZoomControls(true); //允许放大缩小 setting.setDisplayZoomControls(false); //去掉放大缩小框 setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); setting.setSupportMultipleWindows(false); setting.setGeolocationEnabled(true); //允许启用地理定位 setting.setGeolocationDatabasePath(getActivity().getDir("geolocation", 0).getPath()); setting.setSaveFormData(true); //支持保存自动填充的表单数据 setting.setDomStorageEnabled(true); //支持DOM缓存 setting.setDatabaseEnabled(true); setting.setAppCacheEnabled(true); setting.setAppCacheMaxSize(Long.MAX_VALUE); setting.setAppCachePath(getActivity().getDir("dainty_cache", 0).getPath()); // 全屏显示 setting.setUseWideViewPort(true); setting.setTextZoom(Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getContext()).getString("text_size", "100"))); }
Example #10
Source File: WrapperWebView.java From AndroidHybridLib with Apache License 2.0 | 5 votes |
/** * 设置UserAgent * * @param appId * @param versionCode * @param language */ public void setUserAgent(String appId, int versionCode, String language) { if (mIsWhiteList) { WebSettings ws = mWebView.getSettings(); StringBuilder uaSB = new StringBuilder(); uaSB.append(ws.getUserAgentString()); uaSB.append(appId); uaSB.append("."); uaSB.append(versionCode); uaSB.append(HybridConstant.USER_AGENT_LAN); uaSB.append(language); ws.setUserAgentString(uaSB.toString()); } }
Example #11
Source File: DetailActivity.java From KotlinMVPRxJava2Dagger2GreenDaoRetrofitDemo with Apache License 2.0 | 5 votes |
private void webViewSetting() { /* 设置支持Js,必须设置的,不然网页基本上不能看 */ mWebView.getSettings().setJavaScriptEnabled(true); /* 设置缓存模式,我这里使用的默认,不做多讲解 */ mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); /* 设置为true表示支持使用js打开新的窗口 */ mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); /* 大部分网页需要自己保存一些数据,这个时候就的设置下面这个属性 */ mWebView.getSettings().setDomStorageEnabled(true); /* 设置为使用webview推荐的窗口 */ mWebView.getSettings().setUseWideViewPort(true); /* 设 置网页自适应屏幕大小 ---这个属性应该是跟上面一个属性一起用 */ mWebView.getSettings().setLoadWithOverviewMode(true); /* HTML5的地理位置服务,设置为true,启用地理定位 */ mWebView.getSettings().setGeolocationEnabled(true); /* 设置是否允许webview使用缩放的功能,我这里设为false,不允许 */ mWebView.getSettings().setBuiltInZoomControls(false); /* 提高网页渲染的优先级 */ mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); /* 设置显示水平滚动条,就是网页右边的滚动条.我这里设置的不显示 */ mWebView.setHorizontalScrollBarEnabled(false); /* 指定垂直滚动条是否有叠加样式 */ mWebView.setVerticalScrollbarOverlay(true); /* 设置滚动条的样式 */ mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onReceivedTitle(WebView webView, String s) { super.onReceivedTitle(webView, s); if (s.length() > 15) { s = s.substring(0, 15) + "..."; } collapsingtoolbarlayout.setTitle(s); } }); }
Example #12
Source File: x5_MainActivity.java From styT with Apache License 2.0 | 5 votes |
@SuppressLint("SetJavaScriptEnabled") private void initWebSettings() { WebSettings settings = sMm.getSettings(); //sMm.setLayerType(); sMm.setDrawingCacheEnabled(true); //支持获取手势焦点 sMm.requestFocusFromTouch(); //支持JS settings.setJavaScriptEnabled(true); //支持插件 //settings.setUserAgentString("Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en-us) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3 XiaoMi/MiuiBrowser/8.9.4");//UA // settings.setPluginState(WebSettings.PluginState.ON); //设置适应屏幕 settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); //支持缩放 settings.setSupportZoom(false); // 支持缩放 //隐藏原生的缩放控件 settings.setDisplayZoomControls(false); //支持内容重新布局 settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); settings.supportMultipleWindows(); settings.setSupportMultipleWindows(false); //设置缓存模式 settings.setGeolocationEnabled(true);//允许地理位置可用 settings.setDomStorageEnabled(true); settings.setDatabaseEnabled(true); settings.setCacheMode(WebSettings.LOAD_DEFAULT); settings.setAppCacheEnabled(true); settings.setAppCachePath(sMm.getContext().getCacheDir().getAbsolutePath()); //settings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染的优先级 //设置可访问文件 settings.setAllowFileAccess(true); //当webview调用requestFocus时为webview设置节点 settings.setNeedInitialFocus(true); }
Example #13
Source File: CrazyDailyWebView.java From CrazyDaily with Apache License 2.0 | 4 votes |
@SuppressWarnings("all") private void init(Context context) { WebSettings setttings = getSettings(); setttings.setJavaScriptEnabled(true);//打开js setttings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//设置布局 setttings.setDomStorageEnabled(true);//打开Dom Storage setttings.setDatabaseEnabled(true);//打开Database setttings.setAppCacheEnabled(true);//打开App Cache setttings.setAppCacheMaxSize(Long.MAX_VALUE); File cacheDir = new File(context.getExternalCacheDir(), CacheConstant.CACHE_DIR_WEB); if (!cacheDir.exists()) { cacheDir.mkdirs(); } setttings.setAppCachePath(cacheDir.getAbsolutePath());//设置App Cache缓存目录 setttings.setSupportMultipleWindows(false);//不支持多窗口 setttings.setJavaScriptCanOpenWindowsAutomatically(true);//支持js打开新窗口 setttings.setAllowFileAccess(true);//启用WebView访问文件数据 setttings.setSupportZoom(true);//支持缩放 setttings.setDisplayZoomControls(false);//隐藏webview缩放按钮 setttings.setBuiltInZoomControls(true);//支持手势缩放 setttings.setLoadWithOverviewMode(true);//缩放至屏幕大小 setttings.setUseWideViewPort(true);//调整屏幕自适应 setttings.setDefaultTextEncodingName("utf-8");//设置编码格式为utf-8 setttings.setLoadsImagesAutomatically(true);//支持自动加载图片 setttings.setSavePassword(false);//禁止密码保存在本地 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // https中支持访问http setttings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 支持第三方的cookie同步 CookieManager.getInstance().setAcceptThirdPartyCookies(this, true); } String ua = setttings.getUserAgentString(); setttings.setUserAgentString(String.format("%s CrazyDaily %s", ua, DeviceUtil.getVersionName()));//重置ua setWebViewClient(new CrazyDailyWebViewClient()); setWebChromeClient(new CrazyDailyWebChromeClient()); setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { if (mDownloadCallback != null) { mDownloadCallback.onDownload(url, contentLength); } } }); }
Example #14
Source File: EBrowserSetting.java From appcan-android with GNU Lesser General Public License v3.0 | 4 votes |
public void initBaseSetting(boolean webApp) { mWebApp = webApp; mWebSetting.setSaveFormData(false); mWebSetting.setSavePassword(false); mWebSetting.setLightTouchEnabled(false); mWebSetting.setJavaScriptEnabled(true); mWebSetting.setNeedInitialFocus(false); mWebSetting.setSupportMultipleWindows(false); mWebSetting.setGeolocationEnabled(true); // mWebSetting.setNavDump(false); //mWebSetting.setPluginsEnabled(true); mWebSetting.setJavaScriptCanOpenWindowsAutomatically(false); mWebSetting.setUseWideViewPort(false); mWebSetting.setLoadsImagesAutomatically(true); mWebSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); mWebSetting.setUserAgentString(USERAGENT_NEW); mWebSetting.setRenderPriority(RenderPriority.HIGH); mWebSetting.setDefaultTextEncodingName("UTF-8"); if (Build.VERSION.SDK_INT <= 7) { invokeHtml5(mWebSetting); } if (webApp) { mWebSetting.setUseWideViewPort(true); // mWebSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); return; } // mWebSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); // disables the actual onscreen controls from showing up mWebSetting.setBuiltInZoomControls(false); // disables the ability to zoom mWebSetting.setSupportZoom(false); mWebSetting.setDefaultFontSize(ESystemInfo.getIntence().mDefaultFontSize); mWebSetting.setDefaultFixedFontSize(ESystemInfo.getIntence().mDefaultFontSize); // float sca = mBrwView.getScale(); // if (sca != 1.0f) { if (Build.VERSION.SDK_INT <= 18) { mWebSetting.setDefaultZoom(ESystemInfo.getIntence().mDefaultzoom.getValue()); } // } }
Example #15
Source File: WebViewSdkCompat.java From appcan-android with GNU Lesser General Public License v3.0 | 4 votes |
public WebSettings.ZoomDensity getValue() { return value; }
Example #16
Source File: WebViewSdkCompat.java From appcan-android with GNU Lesser General Public License v3.0 | 4 votes |
ZoomDensityCompat(WebSettings.ZoomDensity size) { value = size; }
Example #17
Source File: X5WvWebView.java From YCWebView with Apache License 2.0 | 4 votes |
/** * 做一些公共的初始化操作 */ private void initWebViewSettings() { WebSettings ws = this.getSettings(); // 网页内容的宽度是否可大于WebView控件的宽度 ws.setLoadWithOverviewMode(false); // 保存表单数据 ws.setSaveFormData(true); // 是否应该支持使用其屏幕缩放控件和手势缩放 ws.setSupportZoom(true); // 设置内置的缩放控件。若为false,则该WebView不可缩放 ws.setBuiltInZoomControls(true); // 隐藏原生的缩放控件 ws.setDisplayZoomControls(false); // 启动应用缓存 ws.setAppCacheEnabled(true); // 设置缓存模式 // 缓存模式如下: // LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 // LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。 // LOAD_NO_CACHE: 不使用缓存,只从网络获取数据. // LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。 ws.setCacheMode(WebSettings.LOAD_DEFAULT); ws.setAppCacheMaxSize(Long.MAX_VALUE); // setDefaultZoom api19被弃用 // 设置此属性,可任意比例缩放。 ws.setUseWideViewPort(true); // 告诉WebView启用JavaScript执行。默认的是false。 // 注意:这个很重要 如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript ws.setJavaScriptEnabled(true); // 页面加载好以后,再放开图片 //ws.setBlockNetworkImage(false); // 使用localStorage则必须打开 ws.setDomStorageEnabled(true); //防止中文乱码 ws.setDefaultTextEncodingName("UTF -8"); /* * 排版适应屏幕 * 用WebView显示图片,可使用这个参数 * 设置网页布局类型: 1、LayoutAlgorithm.NARROW_COLUMNS : * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕,内容将自动缩放 */ ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); // WebView是否新窗口打开(加了后可能打不开网页) //ws.setSupportMultipleWindows(true); // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ws.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } //设置字体默认缩放大小 ws.setTextZoom(100); // 不缩放 this.setInitialScale(100); if(Build.VERSION.SDK_INT >= KITKAT) { //设置网页在加载的时候暂时不加载图片 ws.setLoadsImagesAutomatically(true); } else { ws.setLoadsImagesAutomatically(false); } //默认关闭硬件加速 setOpenLayerType(false); //默认不开启密码保存功能 setSavePassword(false); }
Example #18
Source File: X5WebView.java From YCWebView with Apache License 2.0 | 4 votes |
/** * 做一些公共的初始化操作 */ private void initWebViewSettings() { WebSettings ws = this.getSettings(); // 网页内容的宽度是否可大于WebView控件的宽度 ws.setLoadWithOverviewMode(false); // 保存表单数据 ws.setSaveFormData(true); // 是否应该支持使用其屏幕缩放控件和手势缩放 ws.setSupportZoom(true); // 设置内置的缩放控件。若为false,则该WebView不可缩放 ws.setBuiltInZoomControls(true); // 隐藏原生的缩放控件 ws.setDisplayZoomControls(false); // 启动应用缓存 ws.setAppCacheEnabled(true); // 设置缓存模式 // 缓存模式如下: // LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 // LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。 // LOAD_NO_CACHE: 不使用缓存,只从网络获取数据. // LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。 ws.setCacheMode(WebSettings.LOAD_DEFAULT); ws.setAppCacheMaxSize(Long.MAX_VALUE); // setDefaultZoom api19被弃用 // 设置此属性,可任意比例缩放。 ws.setUseWideViewPort(true); // 告诉WebView启用JavaScript执行。默认的是false。 // 注意:这个很重要 如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript ws.setJavaScriptEnabled(true); // 页面加载好以后,再放开图片 //ws.setBlockNetworkImage(false); // 使用localStorage则必须打开 ws.setDomStorageEnabled(true); //防止中文乱码 ws.setDefaultTextEncodingName("UTF-8"); /* * 排版适应屏幕 * 用WebView显示图片,可使用这个参数 * 设置网页布局类型: 1、LayoutAlgorithm.NARROW_COLUMNS : * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕,内容将自动缩放 */ ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); // WebView是否新窗口打开(加了后可能打不开网页) //ws.setSupportMultipleWindows(true); // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ws.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } //设置字体默认缩放大小 ws.setTextZoom(100); // 不缩放 this.setInitialScale(100); if(Build.VERSION.SDK_INT >= KITKAT) { //设置网页在加载的时候暂时不加载图片 ws.setLoadsImagesAutomatically(true); } else { ws.setLoadsImagesAutomatically(false); } //默认关闭硬件加速 setOpenLayerType(false); //默认不开启密码保存功能 setSavePassword(false); //移除高危风险js监听 setRemoveJavascriptInterface(); }
Example #19
Source File: x5_WebviewSelfAdaption.java From stynico with MIT License | 4 votes |
public void getWebviewAdaption(WebView webView) { WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setUseWideViewPort(true);//关键点 //启用地理定位 webSettings.setDomStorageEnabled(true); webSettings.setDatabaseEnabled((Boolean) SPUtils.get(context, "if_4", true));//数据库 webSettings.setGeolocationEnabled((Boolean) SPUtils.get(context, "if_2", true));//地理 webSettings.setUserAgentString("" + SPUtils.get(context, "if_7", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en-us) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3 XiaoMi/MiuiBrowser/8.9.4"));//UA webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webSettings.setDisplayZoomControls(false); webSettings.setLoadsImagesAutomatically((Boolean) SPUtils.get(context, "if_5", true));//图片 webSettings.setJavaScriptEnabled((Boolean) SPUtils.get(context, "if_1", true)); // 设置支持javascript脚本 webSettings.setAllowFileAccess(true); // 允许访问文件 webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮 webSettings.setSupportZoom((Boolean) SPUtils.get(context, "if_3", false)); // 支持缩放 webSettings.setLoadWithOverviewMode(true); DisplayMetrics metrics = new DisplayMetrics(); //getWindowManager().getDefaultDisplay().getMetrics(metrics); int mDensity = metrics.densityDpi; // Log.d("maomao", "densityDpi = " + mDensity); if (mDensity == 240) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else if (mDensity == 160) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); } else if (mDensity == 120) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE); } else if (mDensity == DisplayMetrics.DENSITY_XHIGH) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else if (mDensity == DisplayMetrics.DENSITY_TV) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else { webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); } webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); }
Example #20
Source File: WebviewActivity.java From AndroidFrame with Apache License 2.0 | 4 votes |
private void initWebview() { mWebView = findViewById(R.id.webview); mBridge = new WebViewJavascriptBridge(this, sHandler, mWebView); mWebView.addJavascriptInterface(mBridge, "AndroidWebViewJavascriptBridge"); WebSettings webSetting = mWebView.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); // webSetting.setLoadWithOverviewMode(true); webSetting.setAppCacheEnabled(true); // webSetting.setDatabaseEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); String userAgent = webSetting.getUserAgentString() + " " + AGENG_STR + AGENG_STR_SUFFIX + "/" + getVersionName() + "/QD" + getQudaoName() + "/"; webSetting.setUserAgentString(userAgent); mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView webView, String s) { InputStream fs = WebviewActivity.this.getResources().openRawResource( R.raw.jsbridge_init); String jsbridgeInitScript = getLocalText(fs); String js1 = "javascript:var evtAndroidJSB = document.createEvent('Event');" + "evtAndroidJSB.initEvent('AndroidWebViewJavascriptBridgeReady', false, false); " + "document.dispatchEvent(evtAndroidJSB);"; String js2 = "javascript: " + jsbridgeInitScript; if (android.os.Build.VERSION.SDK_INT <= 18) { mWebView.loadUrl(js1); mWebView.loadUrl(js2); } else { mWebView.evaluateJavascript(js1, null); mWebView.evaluateJavascript(js2, null); } super.onPageFinished(webView, s); } }); }