android WebView 问题积累总结
2013-02-22 14:16
369 查看
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展示出来
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展示出来
相关文章推荐
- Android WebView系列文章4 WebView的问题总结
- Android WebView常见问题总结
- Android WebView 使用和问题总结
- Android WebView常见问题的解决方案总结----例如Web page not available
- Android WebView 使用过程中遇到的问题与总结
- Android开发问题积累 <加载在线Gif><WebView无法加载网页图片>
- Android WebView或手机浏览器打开连接问题解决办法总结
- Android开发问题积累 <加载在线Gif><WebView无法加载网页图片>
- Android WebView常见问题的解决方案总结----例如Web page not available
- Android WebView常见问题总结
- android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
- 关于android webview 设置cookie的问题
- 解决Android WebView 乱码问题
- Android WebView开发问题及优化汇总
- android4.4以上 webview加在图片的缩放问题
- Android混合开发教程之WebView的使用方法总结
- android webview 页面,输入框软键盘遮挡问题(滑动页面)
- Android 4.2 webview注入js对象时需要注意的问题
- Android WebView使用总结
- Android中关于ScrollView中有视频播放器VideoView时问题的总结