您的位置:首页 > 移动开发

Android webview 一些奇怪的问题

2014-07-02 18:42 381 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近在做Android webview 控件相关的开发,发现一下问题,这里做一下总结:</span>


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(includeDiskFiles) Clears
the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.


Parameters

includeDiskFilesif 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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android webview