Android webview 一些奇怪的问题

本文探讨了Android WebView在处理缓存时遇到的奇怪问题,包括即使调用`clearCache(true)`仍可能存在缓存文件的情况,以及在特定操作下可能导致`SIGSEGV`错误。另外,文章还分析了`WebviewClient`中`onPageStarted`和`onPageFinished`方法多次触发的场景,指出这两个方法可能因页面重定向而在加载过程中成对出现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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(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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值