最近在做Android webview 控件相关的开发,发现一下问题,这里做一下总结:

1.关于清除webview缓存的事(搜索相关文章)

   这个有个非常奇怪的问题,为了不使用缓冲,将webview设置如下

WebSettings webSettings = mWebView.getSettings();webSettings.setJavaScriptEnabled(true);webSettings.setAllowContentAccess(false);webSettings.setAllowFileAccess(false);webSettings.setSaveFormData(false);webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);mWebView.setWebViewClient(new myWebViewClient());mWebView.setWebChromeClient(new myChromeClient());

WebSettings.LOAD_NO_CACHE   Don't use the cache, load from the network. 

使用webview.loadUrl(url),退出后查看data/data/your.packagename/cache目录下不存在缓存文件。ok,这个符合我们的设置。

现在在退出程序之前调用webview.clearCache(true);再次查看data/data/your.packagename/cache发现存在缓存文件

webview.clearCache(includeDiskFilesClears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.

Parameters
includeDiskFiles if false, only the RAM cache is cleared
然后使用删除文件的方式将缓存在 data/data/your.packagename/cache删除

private int clearCacheFolder(File dir) {int deletedFiles = 0;if (dir != null && dir.isDirectory()) {try {for (File child : dir.listFiles()) {if (child.isDirectory()) {deletedFiles += clearCacheFolder(child);}printLog("dele file name:" + child.getAbsolutePath() + "/"+ child.getName());if (child.delete()) {deletedFiles++;}}} catch (Exception e) {e.printStackTrace();}}return deletedFiles;}

先调用webview.clearCache(true); 再调用 clearCacheFolder

使用一个for循环不断的webview.loadUrl(); 不断的clearCache(true)  clearCacheFolder,有时候会出现

 Fatal signal 11 (SIGSEGV) at * 0x00000020 (code=1), thread 12500 (WebViewCoreThre) 错误


2.有关于WebviewClient 中的onPageStarted onPageFinished 方法多次触发的问题

API:

onPageStarted

Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.

onPageFinished 

Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).

根据以上说明,我们可以理解onPageFinished触发多次,但是onPageStarted onPageFinished在以下情况下成对触发

webSettings.setJavaScriptEnabled(true);
将WebView 的webSettings.setJavaScriptEnabled(false);

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

 onPageStarted:http://www.baidu.com/   ->  onPageFinished:http://www.baidu.com/

onPageStarted:http://m.baidu.com/?nbid=1F4C6A65309FB23F9C1605638664E35D&from=844b&vit=fps&pu=sz%401321_480&t_noscript=jump

--->

onPageFinished:http://m.baidu.com/?nbid=1F4C6A65309FB23F9C1605638664E35D&from=844b&vit=fps&pu=sz%401321_480&t_noscript=jump


更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Qt on Android(安卓)Episode 3
  6. 使用原始的HTTP拼凑请求的方式上传多张图片
  7. Android文件下载功能实现代码
  8. gen already exists but is not a source folder
  9. ubuntu 18.04编译Android(安卓)7.1源码

随机推荐

  1. Android build.gradle buildConfigField
  2. Android-基本控件(SeekBar 可拖动 滚动条
  3. VMware安装Android虚拟机及adb调试
  4. Android Fragment简记
  5. android平台上的sqllite_简介
  6. android特效展示一:ListView
  7. android WiFi ASSOC_REJECT 流程跟踪
  8. Ubuntu 16.04编译Android,make 版本过高
  9. 针对Android 模拟器启动慢的问题
  10. Android NDK开发之旅38--FFmpeg视频添加