当前位置:网站首页>Android HTML5 page load cache optimization

Android HTML5 page load cache optimization

2022-06-12 02:13:00 Five wood

Program ape everyday

 Hard pressed programmers should be nice to themselves !

LZCache Use

https://github.com/wugemu/WebTest

Application In the initialization The code is as follows :

// initialization 
List<String> cacheList=new ArrayList<String>();// Cached resources 
cacheList.add(".js");
cacheList.add(".css");
cacheList.add(".ico");
cacheList.add(".png");// picture 
cacheList.add(".jpg");
cacheList.add(".gif");
cacheList.add(".action?");// Page request 
CacheUtil.initOnlyCache(512 * 1024 * 1024, cacheList);// Maximum cache 512MB

notes : Discuss What data is cached

initOnlyCache(long cacheMax,List<String> cacheList) 

cacheMax Maximum cache capacity , Only right cacheList Cache files in ( The string contains )

initAllCache(long cacheMax,List<String> noCacheList)

cacheMax Maximum cache capacity , Only right noCacheList Files in are not cached ( The string contains )

1 First load optimization

CacheUtil.loadUrl(WebView webview, Content content, String url, Map<String ,String> headpara);

This method realizes webview Loading and html Content requests are asynchronous ,html The content will be displayed immediately after it is obtained , There is no need to wait for the resource to load , And cache it to the local to synchronize updates , Using this method, offline mode can be realized ( The cache file is completely ).

notes : html5 When there is a request in , Cross domain settings are required ;

2 Cache mechanism optimization

webView.setWebViewClien(new WebViewClientCache());

The sample code is as follows :

webView.setWebViewClient(new WebViewClientCache(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }   

});

原网站

版权声明
本文为[Five wood]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120202104088.html