浅析:Android WebView 常见的九个问题
来源:互联网 发布:小黑屋老版写作软件 编辑:程序博客网 时间:2024/06/05 18:07
目前Android WebView问题越来越多,接下来由爱内测(www.ineice.com)的技术工程师为我们介绍几种常见的Android WebView问题:
1.为WebView自定义错误显示界面:
/**
* 显示自定义错误提示页面,用一个View覆盖在WebView
*/
protected void showErrorPage() {
LinearLayout webParentView = (LinearLayout)mWebView.getParent();
initErrorPage();
while (webParentView.getChildCount() > ) {
webParentView.removeViewAt( );
}
LinearLayout.LayoutParams lp = newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
webParentView.addView(mErrorView, , lp);
mIsErrorPage = true ;
}
protected void hideErrorPage() {
LinearLayout webParentView = (LinearLayout)mWebView.getParent();
mIsErrorPage = false ;
while (webParentView.getChildCount() > ) {
webParentView.removeViewAt( );
}
}
protected void initErrorPage() {
if (mErrorView == null ) {
mErrorView = View.inflate( this , R.layout.online_error,null );
Button button = (Button)mErrorView.findViewById(R.id.online_error_btn_retry);
button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
mWebView.reload();
}
});
mErrorView.setOnClickListener( null );
}
}
2.WebView cookies清理:
CookieSyncManager.createInstance( this );
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().removeSessionCookie();
3.清理cache 和历史记录:
webView.clearCache( true );
webView.clearHistory();
4.判断WebView是否已经滚动到页面底端:
getScrollY()方法返回的是当前可见区域的顶端距整个页面顶端的距离,也就是当前内容滚动的距离.
getHeight()或者getBottom()方法都返回当前WebView这个容器的高度
getContentHeight 返回的是整个html 的高度,但并不等同于当前整个页面的高度,因为WebView 有缩放功能, 所以当前整个页面的高度实际上应该是原始html 的高度再乘上缩放比例. 因此,更正后的结果,准确的判断方法应该是:
if (WebView.getContentHeight*WebView.getScale()== (webview.getHeight()+WebView.getScrollY())){ //已经处于底端 }
5.URL拦截:
Android WebView是拦截不到页面内的fragment跳转的。但是url跳转的话,又会引起页面刷新,H5页面的体验又下降了。只能给WebView注入JS方法了。
6.处理WebView中的非超链接请求(如Ajax请求):
有时候需要加上请求头,但是非超链接的请求,没有办法再shouldOverrinding中拦截并用webView.loadUrl(Stringurl,HashMap headers)方法添加请求头
目前用了一个临时的办法解决:
首先需要在url中加特殊标记/协议, 如在onWebViewResource方法中拦截对应的请求,然后将要添加的请求头,以get形式拼接到url末尾
在shouldInterceptRequest()方法中,可以拦截到所有的网页中资源请求,比如加载JS,图片以及Ajax请求等等
Ex:
@SuppressLint ("NewApi" )
@Override
public WebResourceResponseshouldInterceptRequest(WebView view,String url) {
// 非超链接(如Ajax)请求无法直接添加请求头,现拼接到url末尾,这里拼接一个imei作为示例
String ajaxUrl = url;
// 如标识:req=ajax
if (url.contains("req=ajax" )) {
ajaxUrl +="&imei=" + imei;
}
return super.shouldInterceptRequest(view, ajaxUrl);
}
7.在页面中先显示图片:
@Override
public voidonLoadResource(WebView view, String url) {
mEventListener.onWebViewEvent(CustomWebView. this ,OnWebViewEventListener.EVENT_ON_LOAD_RESOURCE, url);
if (url.indexOf(".jpg" ) > ) {
hideProgress(); //请求图片时即显示页面
mEventListener.onWebViewEvent(CustomWebView. this ,OnWebViewEventListener.EVENT_ON_HIDE_PROGRESS, view.getUrl());
}
super .onLoadResource(view,url);
}
8.屏蔽掉长按事件 因为webview长按时将会调用系统的复制控件:
mWebView.setOnLongClickListener(new OnLongClickListener() {
@Override
public booleanonLongClick(View v) {
return true ;
}
});
9.在WebView加入 flash支持:
String temp = "<html><bodybgcolor=/"" + "black"
+ "/"><br/><embed src=/"" + url + "/"width=/"" + "100%"
+ "/"height=/"" + "90%" + "/" scale=/"" +"noscale"
+ "/"type=/"" + "application/x-shockwave-flash"
+ "/"></embed></body></html>" ;
String mimeType ="text/html" ;
String encoding ="utf-8" ;
web.loadDataWithBaseURL("null" , temp, mimeType, encoding, "" );
- 浅析:Android WebView 常见的九个问题
- Android WebView 常见的九个问题
- Android WebView 常见的九个问题
- android webview 常见的
- 浅析Android webview的使用
- webview开发常见的问题
- android webview的常见使用方法
- android的webview问题
- webview的使用及常见的问题
- Android WebView缓存浅析
- Android webview缩放浅析
- WebView加载页面,常见的问题
- webview详解和常见的问题
- Android WebView 常见设置
- 九个编写Dockerfiles的常见错误
- Android使用WebView的问题
- Android APP以及webview夜间模式的浅析
- Android常见的问题
- Stack类的使用
- 122. Best Time to Buy and Sell Stock II
- Codeforces 560B Gerald is into Art(相片摆放模拟)
- C++ STL编程
- c语言笔记
- 浅析:Android WebView 常见的九个问题
- LBP算子总结-需后期补充
- EasyUI combotree树形下拉框--判断树形下拉框对象是否为空
- spring事务定义中,@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED) 这样定义有问题吗?
- 内置的进制转换的方法---小笔记
- ARM Uboot详解
- Java多线程编程之Runnable、Thread简介
- 123. Best Time to Buy and Sell Stock III
- Codeforces Round #313 (Div. 2) C. Geralds Hexagon (六边形最多划分多少个正三角形)