`
zcwfeng
  • 浏览: 99311 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

android WebView 问题积累总结

 
阅读更多

WebView有一个设置滚动条位置的属性:android:scrollbarStyle 可以是insideOverlay可以是outsideOverlay。但是在layout里面怎么设置这个属性都对WebView不起作用。

mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 就可以了java代码里面设置


1.优先缓存
好了,这里你是不是想问:既然这些图片已经存在手机缓存里面了,为什么Webview不能再把它显示出来呢?
这里我们需要设置下:

WebSettings webSettings= webView.getSettings();
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 默认不使用缓存!


LOAD_CACHE_ELSE_NETWORK的意思是:

Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network. Use withsetCacheMode(int).
如果内容已经存在cache 则使用cache,即使是过去的历史记录。如果cache中不存在,从网络中获取!


2.缓存管理:
(1)clearCacheFolder(Activity.getCacheDir(), System.currentTimeMillis());//删除此时之前的缓存

  • // clear the cache before time numDays
  • private int clearCacheFolder(File dir, long numDays) {
  • int deletedFiles = 0;
  • if (dir!= null && dir.isDirectory()) {
  • try {
  • for (File child:dir.listFiles()) {
  • if (child.isDirectory()) {
  • deletedFiles += clearCacheFolder(child, numDays);
  • }
  • if (child.lastModified() < numDays) {
  • if (child.delete()) {
  • deletedFiles++;
  • }
  • }
  • }
  • } catch(Exception e) {
  • e.printStackTrace();
  • }
  • }
  • return deletedFiles;
  • }(2) 退出应用前删除缓存的方法!

  • File file = CacheManager.getCacheFileBaseDir();
  •   if (file != null && file.exists() && file.isDirectory()) { for (File item : file.listFiles()) { item.delete();
  •   }
  •   file.delete();
  •   }
  •   context.deleteDatabase("webview.db");
  •   context.deleteDatabase("webviewCache.db");


    问题的来历:我加载一个页面的时候,想要在绘屏展示之前取得要加载的内容,并对内容做一些特定的处理。然后再通过loadData来展示修改的内容。原来 使用缓存,也就是CacheManager.CacheResult取得数据,但是也会遇到没有缓存的情况,就是取不到缓存。这时候我就想使用方法直接取 得webview内存的加载的内容。可是不知道怎么去获取。

    昨天已经解决了这个问题,具体的做法是在onPageFinished()的时候,通过loadUrl()加载一段javascript,这段脚本的作用 就是通过DOM树取得Body体的内容,再把这些内容作为字符串传到java层的回调函数里面,这样在回调函数里面处理取得的内容,处理完毕,再 loadData展示出来

  • 分享到:
    评论

    相关推荐

    Global site tag (gtag.js) - Google Analytics