Android 日常开发(28) Webview缓存设置以及缓存清理办法

前言

最近项目中用到这个东西,这里就顺便扯一扯

一般配置

 this.setInitialScale(1);
        this.getSettings().setJavaScriptEnabled(true);
        this.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    this.getSettings().setUseWideViewPort(true);
        this.getSettings().setDisplayZoomControls(true);

设置Webview缓存


        this.getSettings().setDomStorageEnabled(true);//开启 DOM storage API 功能
        this.getSettings().setDatabaseEnabled(true);//开启 database storage API 功能
        this.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存
        String cacheDirPath = getContext().getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME;
        this.getSettings().setAppCacheEnabled(true);

        //设置  Application Caches 缓存目录
        this.getSettings().setAppCachePath(cacheDirPath);

缓存加载策略

{LOAD_DEFAULT, //默认加载方式
LOAD_NORMAL, //废弃
LOAD_CACHE_ELSE_NETWORK,//优先缓存
 LOAD_NO_CACHE,//优先网络
  LOAD_CACHE_ONLY//只使用缓存}

清理Webview缓存

  boolean clearState =  SharedPref.getInstance(getContext()).getBoolean(CLEAR_WEBVIEW_CACHE,false);
        if (clearState) {
            clearCache(true);
            SharedPref.getInstance(getContext()).putBoolean(CLEAR_WEBVIEW_CACHE, false);
        }

微信支付,支付宝支付拉起

@Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                url = request.getUrl().toString();
                String scheme = request.getUrl().getScheme();

                if (!TextUtils.isEmpty(scheme)) {
                    if (scheme.contains("weixin") || scheme.contains("alipays") || scheme.contains("tel")) {
                        //类型我目前用到的是微信、支付宝、拨号 三种跳转方式,其他类型自加
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                }
                //之前无法拉起微信支付跟这个有关
                //切记不能使用注释方法进行处理
                //webview.loadurl("www")
                //return true
                return super.shouldOverrideUrlLoading(view, url);
            }

如果使用注释里面的方法,将会导致重定向地址会增加history,这样的话,返回上一页又会再一次重定向到支付页面,如果使用了页面加载策略本地优先!!!特别注意

结束语

这个里面的内容我就不仔细整理了,有问题直接问!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章