WebView全面解析(1)

/**

  • 当下载文件时打开系统自带的浏览器进行下载,当然也可以对捕获到的 url 进行处理在应用内下载。

**/

webView.setDownloadListener(new DownloadListener() {

@Override

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

Uri uri = Uri.parse(url);

Intent intent = new Intent(Intent.ACTION_VIEW, uri);

startActivity(intent);

}

});

4、WebView的常用工具类

4.1 WebSettings:对WebView进行配置和管理。

WebSettings webSettings = webView.getSettings();

//如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript

webSettings.setJavaScriptEnabled(true);

//设置自适应屏幕,两者合用

webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小

webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局

//缩放操作

webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。

webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放

webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

webSettings.setTextZoom(2);//设置文本的缩放倍数,默认为 100

webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染的优先级

webSettings.setStandardFontFamily(“”);//设置 WebView 的字体,默认字体为 “sans-serif”

webSettings.setDefaultFontSize(20);//设置 WebView 字体的大小,默认大小为 16

webSettings.setMinimumFontSize(12);//设置 WebView 支持的最小字体大小,默认为 8

// 5.1以上默认禁止了https和http混用,以下方式是开启

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

//其他操作

webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存

webSettings.setAllowFileAccess(true); //设置可以访问文件

webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口

webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片

webSettings.setDefaultTextEncodingName(“utf-8”);//设置编码格式

webSettings.setGeolocationEnabled(true);//允许网页执行定位操作

webSettings.setUserAgentString(“Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0”);//设置User-Agent

//不允许访问本地文件(不影响assets和resources资源的加载)

webSettings.setAllowFileAccess(false);

webSettings.setAllowFileAccessFromFileURLs(false);

webSettings.setAllowUniversalAccessFromFileURLs(false);

其中,WebView设置缓存,当加载 html 页面时,WebView会在/data/data/package/下生成 database 与 cache 两个文件夹

请求的URL记录保存在WebViewCache.db,而URL的内容是保存在WebViewCache文件夹下:

//缓存模式如下:

//LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据

//LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。

//LOAD_NO_CACHE: 不使用缓存,只从网络获取数据.

//LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。

//优先使用缓存:

webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

//不使用缓存:

webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE)

还有如下几种:

//开启 DOM storage API 功能 较大存储空间,使用简单

settings.setDomStorageEnabled(true);

//设置数据库缓存路径 存储管理复杂数据 方便对数据进行增加、删除、修改、查询 不推荐使用

settings.setDatabaseEnabled(true);

final String dbPath = context.getApplicationContext().getDir(“db”, Context.MODE_PRIVATE).getPath();

settings.setDatabasePath(dbPath);

//开启 Application Caches 功能 方便构建离线APP 不推荐使用

settings.setAppCacheEnabled(true);

final String cachePath = context.getApplicationContext().getDir(“cache”, Context.MODE_PRIVATE).g

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值