android webview 数据流,必知必会 | WebView 的一切都在这儿

原标题:必知必会 | WebView 的一切都在这儿

本文作者

作者:reezy

链接:

https://www.jianshu.com/p/a6f7b391a0b8

本文由作者授权发布。

文章较长,且大部分说明包含在注释中,建议收藏后慢慢看~

1

目录

1. 相关API

1.1. 相关类介绍

1.2. WebView

1.3. WebSettings

1.4. WebViewClient

1.5. WebChromeClient

2.回调顺序

3.视口(viewport)

4.管理 Cookies

5.缓存(Cache)

6.预加载(Preload)

6.与Java交互

8.地理位置(Geolocation)

9.弹框(alert/confirm/prompt/onbeforeunload)

10.全屏(Fullscreen)

11. 内存泄漏

12.参考

2

相关API

相关类介绍

WebResourceRequest添加于API21,封装了一个Web资源的请求信息,包含:请求地址,请求方法,请求头,是否主框架,是否用户点击,是否重定向

WebResourceResponse封装了一个Web资源的响应信息,包含:响应数据流,编码,MIME类型,API21后添加了响应头,状态码与状态描述

WebResourceError添加于API23,封装了一个Web资源的错误信息,包含错误码和描述

CookieManager管理用于WebView的cookies。。

WebViewDatabase存储与管理以下几类浏览数据:

表单自动填充的的用户名与密码

HTTP认证的用户名与密码

曾经输入过的文本(比如自动完成)

WebStorage用于管理WebView提供的JS存储API,比如Application Cache API,Web SQL Database API,HTML5 Web Storage API

GeolocationPermissions用于管理WebView的JS Geolocation API

HttpAuthHandler表示一个HTTP认证请求,提供了方法操作(proceed/cancel)请求

SslErrorHandler表示一个处理SSL错误的请求,提供了方法操作(proceed/cancel)请求

ClientCertRequest表示一个证书请求,提供了方法操作(proceed/cancel/ignore)请求

JsResult用于处理底层JS发起的请求,为客户端提供一些方法指明应进行的操作,比如确认或取消。

WebView

基本

// 获取当前页面的URL

publicString getUrl();

// 获取当前页面的原始URL(重定向后可能当前url不同)

// 就是http headers的Referer参数,loadUrl时为null

publicString getOriginalUrl();

// 获取当前页面的标题

publicString getTitle();

// 获取当前页面的favicon

publicBitmap getFavicon();

// 获取当前页面的加载进度

publicintgetProgress();

// 通知WebView内核网络状态

// 用于设置JS属性`window.navigator.isOnline`和产生HTML5事件`online/offline`

publicvoidsetNetworkAvailable(booleannetworkUp)

// 设置初始缩放比例

publicvoidsetInitialScale(intscaleInPercent);

加载网页

// 加载URL指定的网页

public voidloadUrl( Stringurl);

// 携带http headers加载URL指定的网页

public voidloadUrl( Stringurl, Map< String, String> additionalHttpHeaders);

// 使用POST请求加载指定的网页

public voidpostUrl( Stringurl, byte[] postData);

// 重新加载当前网页

public voidreload();

// 加载内容

public voidloadData( Stringdata, StringmimeType, Stringencoding);

// 使用baseUrl加载内容

public voidloadDataWithBaseURL( StringbaseUrl, Stringdata, StringmimeType, Stringencoding, StringhistoryUrl);

Java

// 注入Java对象

publicvoidaddJavaInterface(Object object, String name);

// 移除已注入的Java对象,下次加载或刷新页面时生效

publicvoidremoveJavaInterface(String name);

// 对传入的JS表达式求值,通过resultCallback返回结果

// 此函数添加于API19,必须在UI线程中调用,回调也将在UI线程

publicvoidevaluateJava(String , ValueCallback resultCallback)

导航(前进后退)

// 复制一份BackForwardList

publicWebBackForwardList copyBackForwardList();

// 是否可后退

publicbooleancanGoBack();

// 是否可前进

publicbooleancanGoForward();

// 是否可前进/后退steps页,大于0表示前进小于0表示后退

publicbooleancanGoBackOrForward(intsteps);

// 后退一页

publicvoidgoBack();

// 前进一页

publicvoidgoForward();

// 前进/后退steps页,大于0表示前进小于0表示后退

publicvoidgoBackOrForward(intsteps);

// 清除当前webview访问的历史记录

publicvoidclearHistory();

网页查找功能

// 设置网页查找结果回调

publicvoidsetFindListener(FindListener listener);

// 异步执行查找网页内包含的字符串并设置高亮,查找结果会回调.

publicvoidfindAllAsync(String find);

// 查找下一个匹配的字符串

publicvoidfindNext(booleanforward);

// 清除网页查找的高亮匹配字符串

publicvoidclearMatches();

截屏/翻页/缩放

// 保存网页(.html)到指定文件

publicvoidsaveWebArchive(String filename);

// 保存网页(.html)到文件

publicvoidsaveWebArchive(String basename, booleanautoname, ValueCallback callback);

// 上翻一页,即向上滚动WebView高度的一半

publicvoidpageUp(booleantop);

// 下翻一页,即向下滚动WebView高度的一半

publicvoidpageDown(booleanbottom);

// 缩放

publicvoidzoomBy(floatfactor);

// 放大

publicbooleanzoomIn();

// 缩放

publicbooleanzoomOut();

其它

// 清除网页缓存,由于内核缓存是全局的因此这个方法不仅仅针对webview而是针对整个应用程序

publicvoidclearCache(booleanincludeDiskFiles);

// 清除自动完成填充的表单数据

publicvoidclearFormData();

// 清除SSL偏好

publicvoidclearSslPreferences();

// 查询文档中是否有图片,查询结果将被发送到msg.getTarget()

// 如果包含图片,msg.arg1 为1,否则为0

publicvoiddocumentHasImages(Message msg);

// 请求最近轻叩(tapped)的 锚点/图像 元素的URL,查询结果将被发送到msg.getTarget()

// msg.getData()中的url是锚点的href属性,title是锚点的文本,src是图像的src

publicvoidrequestFocusNodeHref(Message msg);

// 请求最近触摸(touched)的 图像元素的URL,查询结果将被发送到msg.getTarget()

// msg.getData()中的url是图像链接

publicvoidrequestImageRef(Message msg)

// 清除证书请求偏好,添加于API21

// 在WebView收到`android.security.STORAGE_CHANGED` Intent时会自动清除

publicstaticvoidclearClientCertPreferences(Runnable onCleared)

// 开启网页内容(js,css,html...)调试模式,添加于API19

publicstaticvoidsetWebContentsDebuggingEnabled(booleanenabled)

WebSettings

WebSettings settings = web.getSettings();

// 存储(storage)

// 启用HTML5 DOM storage API,默认值 false

settings.setDomStorageEnabled( true);

// 启用Web SQL Database API,这个设置会影响同一进程内的所有WebView,默认值 false

// 此API已不推荐使用,参考:https://www.w3.org/TR/webdatabase/

settings.setDatabaseEnabled( true);

// 启用Application Caches API,必需设置有效的缓存路径才能生效,默认值 false

// 此API已废弃,参考:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Using_the_application_cache

settings.setAppCacheEnabled( true);

settings.setAppCachePath(context.getCacheDir().getAbsolutePath());

// 定位(location)

settings.setGeolocationEnabled( true);

// 是否保存表单数据

settings.setSaveFormData( true);

// 是否当webview调用requestFocus时为页面的某个元素设置焦点,默认值 true

settings.setNeedInitialFocus( true);

// 是否支持viewport属性,默认值 false

// 页面通过``自适应手机屏幕

settings.setUseWideViewPort( true);

// 是否使用overview mode加载页面,默认值 false

// 当页面宽度大于WebView宽度时,缩小使页面宽度等于WebView宽度

settings.setLoadWithOverviewMode( true);

// 布局算法

settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);

// 是否支持Java,默认值false

settings.setJavaEnabled( true);

// 是否支持多窗口,默认值false

settings.setSupportMultipleWindows( false);

// 是否可用Java(window.open)打开窗口,默认值 false

settings.setJavaCanOpenWindowsAutomatically( false);

// 资源访问

settings.setAllowContentAccess( true); // 是否可访问Content Provider的资源,默认值 true

settings.setAllowFileAccess( true); // 是否可访问本地文件,默认值 true

// 是否允许通过file url加载的Java读取本地文件,默认值 false

settings.setAllowFileAccessFromFileURLs( false);

// 是否允许通过file url加载的Java读取全部资源(包括文件,http,https),默认值 false

settings.setAllowUniversalAccessFromFileURLs( false);

// 资源加载

settings.setLoadsImagesAutomatically( true); // 是否自动加载图片

settings.setBlockNetworkImage( false); // 禁止加载网络图片

settings.setBlockNetworkLoads( false); // 禁止加载所有网络资源

// 缩放(zoom)

settings.setSupportZoom( true); // 是否支持缩放

settings.setBuiltInZoomControls( false); // 是否使用内置缩放机制

settings.setDisplayZoomControls( true); // 是否显示内置缩放控件

// 默认文本编码,默认值 "UTF-8"

settings.setDefaultTextEncodingName( "UTF-8");

settings.setDefaultFontSize( 16); // 默认文字尺寸,默认值16,取值范围1-72

settings.setDefaultFixedFontSize( 16); // 默认等宽字体尺寸,默认值16

settings.setMinimumFontSize( 8); // 最小文字尺寸,默认值 8

settings.setMinimumLogicalFontSize( 8); // 最小文字逻辑尺寸,默认值 8

settings.setTextZoom( 100); // 文字缩放百分比,默认值 100

// 字体

settings.setStandardFontFamily( "sans-serif"); // 标准字体,默认值 "sans-serif"

settings.setSerifFontFamily( "serif"); // 衬线字体,默认值 "serif"

settings.setSansSerifFontFamily( "sans-serif"); // 无衬线字体,默认值 "sans-serif"

settings.setFixedFontFamily( "monospace"); // 等宽字体,默认值 "monospace"

settings.setCursiveFontFamily( "cursive"); // 手写体(草书),默认值 "cursive"

settings.setFantasyFontFamily( "fantasy"); // 幻想体,默认值 "fantasy"

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

// 用户是否需要通过手势播放媒体(不会自动播放),默认值 true

settings.setMediaPlaybackRequiresUserGesture( true);

}

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

// 5.0以上允许加载http和https混合的页面(5.0以下默认允许,5.0+默认禁止)

settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

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

// 是否在离开屏幕时光栅化(会增加内存消耗),默认值 false

settings.setOffscreenPreRaster( false);

}

if(isNetworkConnected(context)) {

// 根据cache-control决定是否从网络上取数据

settings.setCacheMode(WebSettings.LOAD_DEFAULT);

} else{

// 没网,离线加载,优先加载缓存(即使已经过期)

settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

}

// deprecated

settings.setRenderPriority(WebSettings.RenderPriority.HIGH);

settings.setDatabasePath(context.getDir( "database", Context.MODE_PRIVATE).getPath());

settings.setGeolocationDatabasePath(context.getFilesDir().getPath());

通常大部分保持默认值就好了

WebSettings settings = web.getSettings();

// 缓存(cache)

settings.setAppCacheEnabled( true); // 默认值 false

settings.setAppCachePath(context.getCacheDir().getAbsolutePath());

// 存储(storage)

settings.setDomStorageEnabled( true); // 默认值 false

settings.setDatabaseEnabled( true); // 默认值 false

// 是否支持viewport属性,默认值 false

// 页面通过``自适应手机屏幕

settings.setUseWideViewPort( true);

// 是否使用overview mode加载页面,默认值 false

// 当页面宽度大于WebView宽度时,缩小使页面宽度等于WebView宽度

settings.setLoadWithOverviewMode( true);

// 是否支持Java,默认值false

settings.setJavaEnabled( true);

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

// 5.0以上允许加载http和https混合的页面(5.0以下默认允许,5.0+默认禁止)

settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

if(isNetworkConnected(context)) {

// 根据cache-control决定是否从网络上取数据

settings.setCacheMode(WebSettings.LOAD_DEFAULT);

} else{

// 没网,离线加载,优先加载缓存(即使已经过期)

settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

}

WebViewClient

// 拦截页面加载,返回true表示宿主app拦截并处理了该url,否则返回false由当前WebView处理

// 此方法在API24被废弃,不处理POST请求

publicbooleanshouldOverrideUrlLoading(WebView view, String url){

returnfalse;

}

// 拦截页面加载,返回true表示宿主app拦截并处理了该url,否则返回false由当前WebView处理

// 此方法添加于API24,不处理POST请求,可拦截处理子frame的非http请求

@TargetApi(Build.VERSION_CODES.N)

publicbooleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request){

returnshouldOverrideUrlLoading(view, request.getUrl().toString());

}

// 此方法废弃于API21,调用于非UI线程

// 拦截资源请求并返回响应数据,返回null时WebView将继续加载资源

// 注意:API21以下的AJAX请求会走Resource,无法通过此方法拦截

publicWebResourceResponse shouldInterceptRequest(WebView view, String url){

returnnull;

}

// 此方法添加于API21,调用于非UI线程

// 拦截资源请求并返回数据,返回null时WebView将继续加载资源

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

publicWebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request){

returnshouldInterceptRequest(view, request.getUrl().toString());

}

// 页面(url)开始加载

publicvoidonPageStarted(WebView view, String url, Bitmap favicon){

}

// 页面(url)完成加载

publicvoidonPageFinished(WebView view, String url){

}

// 将要加载资源(url)

publicvoidResource(WebView view, String url){

}

// 这个回调添加于API23,仅用于主框架的导航

// 通知应用导航到之前页面时,其遗留的WebView内容将不再被绘制。

// 这个回调可以用来决定哪些WebView可见内容能被安全地回收,以确保不显示陈旧的内容

// 它最早被调用,以此保证WebView.onDraw不会绘制任何之前页面的内容,随后绘制背景色或需要加载的新内容。

// 当HTTP响应body已经开始加载并体现在DOM上将在随后的绘制中可见时,这个方法会被调用。

// 这个回调发生在文档加载的早期,因此它的资源(css,和图像)可能不可用。

// 如果需要更细粒度的视图更新,查看 postVisualStateCallback(long, WebView.VisualStateCallback).

// 请注意这上边的所有条件也支持 postVisualStateCallback(long ,WebView.VisualStateCallback)

publicvoidonPageCommitVisible(WebView view, String url){

}

// 此方法废弃于API23

// 主框架加载资源时出错

publicvoidonReceivedError(WebView view, interrorCode, String deion, String failingUrl){

}

// 此方法添加于API23

// 加载资源时出错,通常意味着连接不到服务器

// 由于所有资源加载错误都会调用此方法,所以此方法应尽量逻辑简单

@TargetApi(Build.VERSION_CODES.M)

publicvoidonReceivedError(WebView view, WebResourceRequest request, WebResourceError error){

if(request.isForMainFrame()) {

onReceivedError(view, error.getErrorCode(), error.getDeion().toString(), request.getUrl().toString());

}

}

// 此方法添加于API23

// 在加载资源(iframe,image,js,css,ajax...)时收到了 HTTP 错误(状态码>=400)

publicvoidonReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse){

}

// 是否重新提交表单,默认不重发

publicvoidonFormResubmission(WebView view, Message dontResend, Message resend){

dontResend.sendToTarget();

}

// 通知应用可以将当前的url存储在数据库中,意味着当前的访问url已经生效并被记录在内核当中。

// 此方法在网页加载过程中只会被调用一次,网页前进后退并不会回调这个函数。

publicvoiddoUpdateVisitedHistory(WebView view, String url, booleanisReload){

}

// 加载资源时发生了一个SSL错误,应用必需响应(继续请求或取消请求)

// 处理决策可能被缓存用于后续的请求,默认行为是取消请求

publicvoidonReceivedSslError(WebView view, SslErrorHandler handler, SslError error){

handler.cancel();

}

// 此方法添加于API21,在UI线程被调用

// 处理SSL客户端证书请求,必要的话可显示一个UI来提供KEY。

// 有三种响应方式:proceed()/cancel()/ignore(),默认行为是取消请求

// 如果调用proceed()或cancel(),Webview 将在内存中保存响应结果且对相同的"host:port"不会再次调用 onReceivedClientCertRequest

// 多数情况下,可通过KeyChain.choosePrivateKeyAlias启动一个Activity供用户选择合适的私钥

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

publicvoidonReceivedClientCertRequest(WebView view, ClientCertRequest request){

request.cancel();

}

// 处理HTTP认证请求,默认行为是取消请求

publicvoidonReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm){

handler.cancel();

}

// 通知应用有个已授权账号自动登陆了

publicvoidonReceivedLoginRequest(WebView view, String realm, String account, String args){

}

// 给应用一个机会处理按键事件

// 如果返回true,WebView不处理该事件,否则WebView会一直处理,默认返回false

publicbooleanshouldOverrideKeyEvent(WebView view, KeyEvent event){

returnfalse;

}

// 处理未被WebView消费的按键事件

// WebView总是消费按键事件,除非是系统按键或shouldOverrideKeyEvent返回true

// 此方法在按键事件分派时被异步调用

publicvoidonUnhandledKeyEvent(WebView view, KeyEvent event){

super.onUnhandledKeyEvent(view, event);

}

// 通知应用页面缩放系数变化

publicvoidonScaleChanged(WebView view, floatoldScale, floatnewScale){

}

WebChromeClient

// 获得所有访问历史项目的列表,用于链接着色。

publicvoidgetVisitedHistory(ValueCallback callback){

}

// 控件在未播放时,会展示为一张海报图,HTML中可通过它的'poster'属性来指定。

// 如果未指定'poster'属性,则通过此方法提供一个默认的海报图。

publicBitmap getDefaultVideoPoster(){

returnnull;

}

// 当全屏的视频正在缓冲时,此方法返回一个占位视图(比如旋转的菊花)。

publicView getVideoLoadingProgressView(){

returnnull;

}

// 接收当前页面的加载进度

publicvoidonProgressChanged(WebView view, intnewProgress){

}

// 接收文档标题

publicvoidonReceivedTitle(WebView view, String title){

}

// 接收图标(favicon)

publicvoidonReceivedIcon(WebView view, Bitmap icon){

}

// Android中处理Touch Icon的方案

// http://droidyue.com/blog/2015/01/18/deal-with-touch-icon-in-android/index.html

publicvoidonReceivedTouchIconUrl(WebView view, String url, booleanprecomposed){

}

// 通知应用当前页进入了全屏模式,此时应用必须显示一个包含网页内容的自定义View

publicvoidonShowCustomView(View view, CustomViewCallback callback){

}

// 通知应用当前页退出了全屏模式,此时应用必须隐藏之前显示的自定义View

publicvoidonHideCustomView(){

}

// 显示一个alert对话框

publicbooleanonJsAlert(WebView view, String url, String message, JsResult result){

returnfalse;

}

// 显示一个confirm对话框

publicbooleanonJsConfirm(WebView view, String url, String message, JsResult result){

returnfalse;

}

// 显示一个prompt对话框

publicbooleanonJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result){

returnfalse;

}

// 显示一个对话框让用户选择是否离开当前页面

publicbooleanonJsBeforeUnload(WebView view, String url, String message, JsResult result){

returnfalse;

}

// 指定源的网页内容在没有设置权限状态下尝试使用地理位置API。

// 从API24开始,此方法只为安全的源(https)调用,非安全的源会被自动拒绝

publicvoidonGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback){

}

// 当前一个调用 onGeolocationPermissionsShowPrompt() 取消时,隐藏相关的UI。

publicvoidonGeolocationPermissionsHidePrompt(){

}

// 通知应用打开新窗口

publicbooleanonCreateWindow(WebView view, booleanisDialog, booleanisUserGesture, Message resultMsg){

returnfalse;

}

// 通知应用关闭窗口

publicvoidonCloseWindow(WebView window){

}

// 请求获取取焦点

publicvoidonRequestFocus(WebView view){

}

// 通知应用网页内容申请访问指定资源的权限(该权限未被授权或拒绝)

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

publicvoidonPermissionRequest(PermissionRequest request){

request.deny();

}

// 通知应用权限的申请被取消,隐藏相关的UI。

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

publicvoidonPermissionRequestCanceled(PermissionRequest request){

}

// 为''显示文件选择器,返回false使用默认处理

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

publicbooleanonShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams){

returnfalse;

}

// 接收Java控制台消息

publicbooleanonConsoleMessage(ConsoleMessage consoleMessage){

returnfalse;

}

3

回调顺序

页面加载回调顺序:

shouldOverrideUrlLoading

onProgressChanged[ 10]

shouldInterceptRequest

onProgressChanged[...]

onPageStarted

onProgressChanged[...]

onLoadResource

onProgressChanged[...]

onReceivedTitle/onPageCommitVisible

onProgressChanged[ 100]

onPageFinished

onReceivedIcon

资源加载回调:

shouldInterceptRequest() -> Resource()

发生重定向时回调:

onPageStarted() -> shouldOverrideUrlLoading()

直接loadUrl的回调:

// 无重定向

onPageStarted() -> onPageFinished()

// 有重定向,shouldOverrideUrlLoading 返回 true 时 onPageFinished 仍会执行

onPageStarted() -> redirection -> ... -> onPageFinished()

用户点击链接的回调:

// shouldOverrideUrlLoading 返回 true 时不执行onPageStarted/onPageFinished

shouldOverrideUrlLoading() -> ...

// 无重定向

shouldOverrideUrlLoading() -> onPageStarted() -> onPageFinished()

// 有重定向

shouldOverrideUrlLoading() -> onPageStarted() -> redirection -> ... -> onPageFinished()

// 有重定向(A->B->C)

shouldOverrideUrlLoading(A) -> onPageStarted(A) ->

onPageStarted(B) -> shouldOverrideUrlLoading(B) ->

onPageStarted(C) -> shouldOverrideUrlLoading(C) -> onPageFinished(C)

后退/前进/刷新 时回调:

onPageStarted() -> onPageFinished()

关于window.location

假设从A页面跳转到B页面

如果页面B中直接输出 window.location="http://example.com",那页面B不会被加入回退栈,回退将直接回到A页

如果页面B加载完成后,比如用setTimeout延迟了,那页面B会被加入回退栈,当回退到页面A时会再执行跳转,这会导致回退功能看起来不正常,需要快速回退两次才能回到A页面

4

视口(viewport)

https://developer.android.com/guide/webapps/targeting.html

https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag

https://developer.mozilla.org/zh-CN/docs/Web/CSS/@viewport

视口是一个为网页提供绘图区域的矩形。

你可以指定数个视口属性,比如尺寸和初始缩放系数(initial scale)。其中最重要的是视口宽度,它定义了网页水平方向的可用像素总数(可用的CSS像素数)。

多数 Android 上的网页浏览器(包括 Chrome)设置默认视口为一个大尺寸(被称为"wide viewport mode",宽约 980px)。

也有许多浏览器默认会尽可能缩小以显示完整的视口宽度(被称为"overview mode")。

// 是否支持viewport属性,默认值 false

// 页面通过``自适应手机屏幕

// 当值为true且viewport标签不存在或未指定宽度时使用 wide viewport mode

settings.setUseWideViewPort( true);

// 是否使用overview mode加载页面,默认值 false

// 当页面宽度大于WebView宽度时,缩小使页面宽度等于WebView宽度

settings.setLoadWithOverviewMode( true);

viewport 语法

content= "

height = [pixel_value | "device-height "] ,

width = [pixel_value | "device-width "] ,

initial-scale = float_value ,

minimum-scale = float_value ,

maximum-scale = float_value ,

user-scalable = ["yes" | "no"]

"/>

指定视口宽度精确匹配设备屏幕宽度同时禁用了缩放

Example

通过WebView设置初始缩放(initial-scale)

// 设置初始缩放百分比

// 0表示依赖于setUseWideViewPort和setLoadWithOverviewMode

// 100表示不缩放

web.setInitialScale( 0)

5

管理 Cookies

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies

Cookie 是服务器发送到用户浏览器并保存在浏览器上的一块数据,它会在浏览器下一次发起请求时被携带并发送到服务器上。

可通过Cookie保存浏览信息来获得更轻松的在线体验,比如保持登录状态、记住偏好设置,并提供本地的相关内容。

会话Cookie 与 持久Cookie

会话cookie不需要指定Expires和Max-Age,浏览器关闭之后它会被自动删除。

持久cookie指定了Expires或Max-Age,会被存储到磁盘上,不会因浏览器而失效。

第一方Cookie 与 第三方Cookie

每个Cookie都有与之关联的域,与页面域一样的就是第一方Cookie,不一样的就是第三方Cookie。

// 设置接收第三方Cookie

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

CookieManager.getInstance().setAcceptThirdPartyCookies(vWeb, true);

}

读取/写入/移除 Cookie

// 获取指定url关联的所有Cookie

// 返回值使用"Cookie"请求头格式:"name=value; name2=value2; name3=value3"

CookieManager.getInstance().getCookie(url);

// 为指定的url设置一个Cookie

// 参数value使用"Set-Cookie"响应头格式,参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Set-Cookie

CookieManager.getInstance().setCookie(url, value);

// 移除指定url下的指定Cookie

CookieManager.getInstance().setCookie(url, cookieName + "=");

webkit cookie 工具类

publicclassWebkitCookieUtil{

// 移除指定url关联的所有cookie

publicstaticvoidremove(String url){

CookieManager cm = CookieManager.getInstance();

for(String cookie : cm.getCookie(url).split( "; ")) {

cm.setCookie(url, cookie.split( "=")[ 0] + "=");

}

flush();

}

// sessionOnly 为true表示移除所有会话cookie,否则移除所有cookie

publicstaticvoidremove(booleansessionOnly){

CookieManager cm = CookieManager.getInstance();

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

if(sessionOnly) {

cm.removeSessionCookies( null);

} else{

cm.removeAllCookies( null);

}

} else{

if(sessionOnly) {

cm.removeSessionCookie();

} else{

cm.removeAllCookie();

}

}

flush();

}

// 写入磁盘

publicstaticvoidflush(){

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

CookieManager.getInstance().flush();

} else{

CookieSyncManager.getInstance().sync();

}

}

}

同步系统Cookie 与 Webkit Cookie

// 将系统级Cookie(比如`new URL(...).openConnection()`的Cookie) 同步到 WebView

public classWebkitCookieHandlerextendsCookieHandler{

private staticfinal StringTAG = WebkitCookieHandler.class.getSimpleName();

private CookieManager wcm;

public WebkitCookieHandler() {

this.wcm = CookieManager.getInstance();

}

@Override

public voidput(URI uri, Map< String, List< String>> headers) throws IOException {

if((uri == null) || (headers == null)) {

return;

}

Stringurl = uri.toString();

for( StringheaderKey : headers.keySet()) {

if((headerKey == null) || !(headerKey.equalsIgnoreCase( "set-cookie2") || headerKey.equalsIgnoreCase( "set-cookie"))) {

continue;

}

for( StringheaderValue : headers.get(headerKey)) {

Log.e(TAG, headerKey + ": "+ headerValue);

this.wcm.setCookie(url, headerValue);

}

}

}

@Override

public Map< String, List< String>> get(URI uri, Map< String, List< String>> headers) throws IOException {

if((uri == null) || (headers == null)) {

thrownewIllegalArgumentException( "Argument is null");

}

Stringurl = uri.toString();

Stringcookie = this.wcm.getCookie(url);

Log.e(TAG, "cookie: "+ cookie);

if(cookie != null) {

returnCollections.singletonMap( "Cookie", Arrays.asList(cookie));

} else{

returnCollections.emptyMap();

}

}

}

6

缓存(Cache)

设置缓存模式

WebSettings.LOAD_DEFAULT 根据cache-control决定是否从网络上取数据

WebSettings.LOAD_CACHE_ELSE_NETWORK 无网,离线加载,优先加载缓存(即使已经过期)

WebSettings.LOAD_NO_CACHE 仅从网络加载

WebSettings.LOAD_CACHE_ONLY 仅从缓存加载

// 网络正常时根据cache-control决定是否从网络上取数据

if(isNetworkConnected(mActivity)) {

settings.setCacheMode(WebSettings.LOAD_DEFAULT);

} else{

settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

}

清除缓存

// 传入 true表示同时内存与磁盘, false表示仅清除内存

// 由于内核缓存是全局的因此这个方法不仅仅针对webview而是针对整个应用程序

web.clearCache( true);

7

预加载(Preload)

一个简单的预加载示例(shouldInterceptRequest)

点击 assets/demo.xml 里的链接"hello"时会加载本地的 assets/hello.html

assets/demo.xml

hello

assets/hello.html

helloworld!

重载 shouldInterceptRequest

@Override

public WebResourceResponse shouldInterceptRequest(WebView view, Stringurl) {

returnpreload( "assets/", url);

}

WebResourceResponse preload( Stringpath, Stringurl) {

if(!url.contains(path)) {

returnnull;

}

Stringlocal = url.replaceFirst( "^http.*"+ path, "");

try{

InputStream is = getApplicationContext().getAssets().open(local);

Stringext = MimeTypeMap.getFileExtensionFromUrl(local);

StringmimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

returnnewWebResourceResponse(mimeType, "UTF-8", is);

} catch(Exception e) {

e.printStackTrace();

returnnull;

}

}

8

与Java交互

启用Java

// 是否支持Java,默认值 false

settings.setJavaEnabled( true);

注入对象到Java

//注入对象 'jsobj',在网页中通过` jsobj.say(...)`调用

web.addJavaInterface( newJSObject(), "jsobj")

在API17后支持白名单,只有添加了@JavaInterface注解的方法才会注入JS

publicclassJSObject{

@JavaInterface

publicvoidsay(String words){

// todo

}

}

移除已注入Java的对象

web.removeJavaInterface( "jsobj")

执行JS表达式

// 弹出提示框

web.loadUrl( "java:alert('hello')");

// 调用注入的jsobj.say方法

web.loadUrl( "java:jsobj.say('hello')");

在API19后可异步执行JS表达式,并通过回调返回值

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

vWeb.evaluateJava( "111+222", newValueCallback() {

@ Override

publicvoidonReceiveValue(String value) {

// value => "333"

}

});

}

9

地理位置(Geolocation)

https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation/Using_geolocation

需要以下权限

默认可用

settings.setGeolocationEnabled( true);

当H5调用地理位置API时,会先通过WebChromeClient.onGeolocationPermissionsShowPrompt申请授权

// 指定源的网页内容在没有设置权限状态下尝试使用地理位置API。

@Override

publicvoidonGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback){

booleanallow = true; // 是否允许origin使用定位API

booleanretain = false; // 内核是否记住这次制授权

callback.invoke(origin, true, false);

}

// 之前调用 onGeolocationPermissionsShowPrompt() 申请的授权被取消时,隐藏相关的UI。

@Override

publicvoidonGeolocationPermissionsHidePrompt(){

}

注:从API24开始,仅支持安全源(https)的请求,非安全源的请求将自动拒绝且不调用 onGeolocationPermissionsShowPrompt 与 onGeolocationPermissionsHidePrompt

10

弹框(alert/confirm/prompt/onbeforeunload)

在java中使用 alert/confirm/prompt 会弹出对话框,可通过重载 WebChromeClient 的下列方法控制弹框的交互,比如替换系统默认的对话框或屏蔽这些对话框

@Override

publicbooleanonJsAlert(WebView view, String url, String message, JsResult result){

// 这里处理交互逻辑

// result.cancel(); 表示用户取消了操作(点击了取消按钮)

// result.confirm(); 表示用户确认了操作(点击了确认按钮)

// ...

// 返回true表示自已处理,返回false表示由系统处理

returnfalse;

}

@Override

publicbooleanonJsConfirm(WebView view, String url, String message, JsResult result){

returnfalse;

}

@Override

publicbooleanonJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result){

returnfalse;

}

@Override

publicbooleanonJsBeforeUnload(WebView view, String url, String message, JsResult result){

returnfalse;

}

11

全屏(Fullscreen)

Fullscreen API

https://developer.mozilla.org/zh-CN/docs/DOM/Using_fullscreen_mode

当H5请求全屏时,会回调 WebChromeClient.onShowCustomView 方法

当H5退出全屏时,会回调 WebChromeClient.onHideCustomView 方法

1.manifest

自己处理屏幕尺寸方向的变化(切换屏幕方向时不重建activity)

WebView播放视频需要开启硬件加速

android:name=".WebViewActivity"

android:configChanges="orientation|screenSize"

android:hardwareAccelerated="true"

android:screenOrientation="portrait"/>

2.页面布局

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/toolbar"

style="@style/Toolbar.Back"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/web"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

...

3.处理全屏回调

CustomViewCallback mCallback;

View vCustom;

@Override

publicvoidonShowCustomView(View view, CustomViewCallback callback){

setFullscreen( true);

vCustom = view;

mCallback = callback;

if(vCustom != null) {

ViewGroup parent = (ViewGroup) vWeb.getParent();

parent.addView(vCustom);

}

}

@Override

publicvoidonHideCustomView(){

setFullscreen( false);

if(vCustom != null) {

ViewGroup parent = (ViewGroup) vWeb.getParent();

parent.removeView(vCustom);

vCustom = null;

}

if(mCallback != null) {

mCallback.onCustomViewHidden();

mCallback = null;

}

}

4.设置全屏,切换屏幕方向

voidsetFullscreen(booleanfullscreen){

if(fullscreen) {

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

vToolbar.setVisibility(View.GONE);

vWeb.setVisibility(View.GONE);

} else{

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

vToolbar.setVisibility(View.VISIBLE);

vWeb.setVisibility(View.VISIBLE);

}

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

} else{

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

}

12

内存泄漏

直接 new WebView 并传入 application context 代替在 XML 里面声明以防止 activity 引用被滥用,能解决90+%的 WebView 内存泄漏。

vWeb = new WebView(getContext().getApplicationContext());

container.addView(vWeb);

注:此方法会导致select无法弹出,因为select默认会弹出一个原生的框,需要activity承载。

W/WindowManager: Attempted to add windowwithnon-application token WindowToken{ 75b7f7c null}. Aborting.

销毁 WebView

if(vWeb != null) {

vWeb.setWebViewClient( null);

vWeb.setWebChromeClient( null);

vWeb.loadDataWithBaseURL( null, "", "text/html", "utf-8", null);

vWeb.clearHistory();

((ViewGroup) vWeb.getParent()).removeView(vWeb);

vWeb.destroy();

vWeb = null;

}

参考

https://developer.android.com/reference/android/webkit/package-summary.html

Fullscreen API 全屏显示网页

http://calefy.org/2012/06/03/fullscreen-web-page-width-fullscreen-api.html

WebView实现全屏播放的一种方法

https://segmentfault.com/a/1190000007561455

第一方Cookie和第三方Cookie区别

https://www.biaodianfu.com/first-party-cookie-and-third-party-cookie.html

Android WebView的Js对象注入漏洞解决方案

http://blog.csdn.net/leehong2005/article/details/11808557

Android安全开发之WebView中的地雷

http://yaq.qq.com/blog/10

Android WebView:性能优化不得不说的事

https://juejin.im/entry/57d6434067f3560057e50b20

上次四哥的书,通过小程序中奖的,其中7位已经提交了相关信息,我已经发给四哥了,还有位胡宸浩一直没有填写,看到请尽快提交,或者后台给我发消息即可~返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值