WebView的使用

1、WebView的作用

在APP开发中可能用到Web端中已经写好的代码,如果想要在APP中显示这样的代码,没有重写的必要,因此使用WebView来显示这些网页。WebView是用来显示网页的控件。
这里写图片描述

2、WebView开发APP的优缺点

优点:
1、跨平台。Android,IOS都可以使用同一套代码,成本低。
2、可以随时更新APP版本,不需要每一次下载更新。

缺点:
1、耗电量问题。
2、加载速度比原生的慢,卡。
3、手机发热问题

3、WebView的使用

1、通过loadUrl()方法加载一个WebView的页面。这种方式加载,会通过系统浏览器打开。

  webView.loadUrl("http://www.baidu.com");

2、通过自己APP中打开WebView中的页面
第一步:仍然loadUrl();

 webView.loadUrl("http://www.baidu.com");

第二步:重写webView.setWebViewClient()方法

 webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

这样,就不会通过系统的浏览器打开了,而是通过APP中启动WebView中的页面。

4、WebView的错误页面的显示

重写onReceivedError()方法

  @Override

            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }

1、通过本地html页面显示错误信息。
2、通过原生代码显示错误页面。

  @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                tv_error.setText("can't find pages");
                webView.setVisibility(View.GONE);
            }

5、Web包View调用JS代码混淆打后,调用出现问题

在打release包的时候,发现调用原来的webview调用js代码出现问题,或者调用不成功。原因是在打包的时候,调用js代码出现错误。
因此在,打包的时候,应该忽略调用的js方法区的代码
在proguard-project中禁止打包时混淆代码

-keepclassmembers class cn.xx.xx.Activity$AppAndroid {  
  public *;  
}  

-keepattributes *Annotation*  
-keepattributes *JavascriptInterface* 

6、WebView的优化问题

1、图片的延迟加载

加载html页面的时候,如果有比较大,或者比较多的图片的时候,会影响到加载的速度。因此可以设置webview先不要加载图片,等页面加载后然后再发起加载图片。

public void int () {
    if(Build.VERSION.SDK_INT >= 19) {
        webView.getSettings().setLoadsImagesAutomatically(true);
    } else {
        webView.getSettings().setLoadsImagesAutomatically(false);
    }
}

在onPageFinished中添加加载图片代码

@Override
public void onPageFinished(WebView view, String url) {
    if(!webView.getSettings().getLoadsImagesAutomatically()) {
        webView.getSettings().setLoadsImagesAutomatically(true);
    }
}

2、WebView缓存问题

设置缓存减小对服务器资源的请求。

/**
         * Default cache usage mode. If the navigation type doesn't impose any
         * specific behavior, use cached resources when they are available
         * and not expired, otherwise load resources from the network.
         * Use with {@link #setCacheMode}.
         */

        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);


        webView.getSettings().setDomStorageEnabled(true);

3、资源文件的本地存储

一些常用的资源文件比如html,js,css等一些不需要更新的文件,可以放在本地加载,而不是通过网络加载。比如说:错误界面的统一处理,固定的格式样式。

7、WebView与ViewPage嵌套问题

如果ViewPage嵌套多个WebView页面,则我们会发现点击事件不管用,或者WebView中内容的一些不响应点击事件。
原因:ViewPage拦截的WebView的点击事件。
解决办法:在WebView中重写onTouchEvent()事件。

@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean ret = super.onTouchEvent(ev);
    if (mPreventParentTouch) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                requestDisallowInterceptTouchEvent(true);
                ret = true;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                requestDisallowInterceptTouchEvent(false);
                mPreventParentTouch = false;
                break;
        }
    }
    return ret;
}

public void preventParentTouchEvent () {
    mPreventParentTouch = true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值