当前位置: 移动技术网 > IT编程>移动开发>Android > Android WebView 常见问题及处理方案

Android WebView 常见问题及处理方案

2019年07月24日  | 移动技术网IT编程  | 我要评论

圣力炼金术士,2008奥运会开幕式下载,花果园失火

目前html5发展非常迅速,很多native app都会嵌入到网页中,以此来适用多变的市场需求。但是android的webview默认支持的功能非常弱,很多地方都是需要自定义的,才能达到我们想要的效果。并且webview在不同的版本会有不同程度的bug。下面小编把webview经常出现的问题给大家整理如下:

1.为webview自定义错误显示界面:

  /**
  * 显示自定义错误提示页面,用一个view覆盖在webview
  */
  protected void showerrorpage() {
  linearlayout webparentview = (linearlayout)mwebview.getparent();
  initerrorpage();
  while (webparentview.getchildcount() > ) {
  webparentview.removeviewat( );
  }
  linearlayout.layoutparams lp = new linearlayout.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(string url,hashmap headers)方法添加请求头
目前用了一个临时的办法解决:
首先需要在url中加特殊标记/协议, 如在onwebviewresource方法中拦截对应的请求,然后将要添加的请求头,以get形式拼接到url末尾
在shouldinterceptrequest()方法中,可以拦截到所有的网页中资源请求,比如加载js,图片以及ajax请求等等

ex:
  @suppresslint ( "newapi" )
  @override
  public webresourceresponse shouldinterceptrequest(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 void onloadresource(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 boolean onlongclick(view v) {
  return true ;
  }
  });

9.在webview加入 flash支持:
  

 string temp = "<html><body bgcolor=/"" + "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 常见问题及处理方案的全部叙述,希望大家喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网