1. 程式人生 > >android清除WebView使用的快取的學習記錄

android清除WebView使用的快取的學習記錄

因為實習的專案的需求,我需要清除專案中使用的WebView控制元件產生快取

先連線上OTG線,開adb shell進行除錯,進入data/data目錄找到安裝的程式的位置;

應用程式包的命名習慣的慣例是:域名.公司或組織名.程式

檔案結構如下:


app_tbs:專案用到的騰訊TBS X5核心產生的檔案

app_textures:似乎是和圖形介面描繪有關的檔案

app_webview:webview控制元件的資料夾

cacahe:快取資料的資料夾

databases:儲存資料的資料夾

files:長時間儲存的資料的資料夾

lib:裡面存放有後綴名為.so的檔案,應該是依賴的庫裡面帶有的;

shared_prefs:應該是存放SharedPreference的目錄;

基於這樣的檔案結構,可以肯定需要刪除的是cache資料夾的內容和databases資料夾裡面的內容,而app_webview則不敢肯定;


以上就是app_webview內的內容,比較明顯的第一反應看到比較熟悉的是Cookies資料夾和GPUCache

根據網上老哥們的說法,Cookies是sqlite,而Cookies-journal則是sqlite的快取;

這兩者應該是不需要清理的;

專案中用到WebView控制元件,在載入html頁面時,會在應用目錄下生成database和cache資料夾;

請求的url記錄則儲存在WebViewCache.db,而url的內容則儲存在WebViewCache資料夾下

那麼根據以上條件,先決定進行對cache資料夾和databases資料夾下的webview.db和webviewCache.db進行清理

 首先使用了檔案刪除的辦法清理快取;

public class DataCleanManager {
	private static DataOutputStream dos = null;
	private static Process p = null;
	
	/**
	 * 保留函式
	 * 執行root許可權下的命令
	 * @param cmd 命令
	 */
	private static void runRootCmd(String cmd){
		try {
			p = Runtime.getRuntime().exec("su");
			dos = new DataOutputStream(p.getOutputStream());
			dos.writeBytes(cmd + "\n");
			dos.flush();
			dos.close();
			p.destroy();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(dos != null){
				try{
					dos.close();
				}catch (IOException e) { e.printStackTrace(); }
			}
		}
	}
	
	/**
	 * 刪除輸入引數目錄下的檔案 
	 * @param directory 檔案目錄
	 */
	private static void deleteFileByDirectory(File directory){
		if(directory != null && directory.exists() && directory.isDirectory()){
			for(File file : directory.listFiles() )
				file.delete();
		}
	}
	
	/**
	 * 刪除目錄下的指定檔名的檔案
	 * @param directory 檔案目錄
	 * @param filename 檔名
	 */
	private static void deleteFileByDirectory(File directory, String filename){
		if(directory != null && directory.exists() && directory.isDirectory()){
			for(File file : directory.listFiles()){
				if(file.toString().equals(filename))
					file.delete();
			}
		}
	}
	
	/**
	 * 清除快取檔案
	 * @param context
	 */
	private void cleanInternalCache(Context context){
		deleteFileByDirectory(context.getCacheDir());
	}
	
	/**
	 * 清除database資料夾下的webview.db和 webviewCache.db
	 * @param context
	 */
	private void cleanDatabases(Context context){
		String dataBasesFilePath = "/data/data" + context.getPackageName() + "/databases";
		deleteFileByDirectory(new File(dataBasesFilePath), "webview.db");
		deleteFileByDirectory(new File(dataBasesFilePath), "webviewCache.db");
	}
	
	/**
	 * 清除webView相關的快取
	 * @param context
	 */
	public void cleanCache(Context context){
		cleanInternalCache(context);
		cleanDatabases(context);
	}
	
}
在專案的實際應用中好像有比較多的問題,刪除了檔案之後會導致專案不穩定;

然後參考了一篇比較好的博文:http://blog.csdn.net/liwei123liwei123/article/details/52624826;

 引用了一下上面那篇博文,再記錄一下要點:

1. WebView快取分為兩部分:

 1.1頁面快取,載入一個網頁時的html,css,JS的頁面資源,這部分快取是因為瀏覽器行為而產生的,開發者只能通過

配置HTTP響應頭影響瀏覽器行為才能間接影響這些資料;

->>這部分快取的索引存放在/data/data/package_name/databases資料夾下

->>這部分快取的檔案存放在/data/data/package_name/cache/XXXwebviewcacheXXX下

1.2資料快取,資料快取分為AppCache和DOM Storage兩種,這些快取資源是由開發者的直接行為產生的;

Android中的Webkit使用一個db檔案來儲存AppCache資料(my_path/ApplicationCache.db)

Android的Webkit會為DOM Storage產生兩個檔案(my_path/localstorage/網頁 和 my_path/localstorage/Databases.db)