我們請求的Url記錄是保存在webviewCache.db里,而url的內容是保存在webviewCache文件夾下.
WebView中存在着兩種緩存:網頁數據緩存(存儲打開過的頁面及資源)、H5緩存(即AppCache)。
一、網頁緩存
1、緩存構成/data/data/package_name/cache/
/data/data/package_name/database/webview.db
/data/data/package_name/database/webviewCache.db
綜合可以得知 webview 會將我們瀏覽過的網頁url已經網頁文件(css、圖片、js等)保存到數據庫表中
緩存模式(5種)
LOAD_CACHE_ONLY: 不使用網絡,只讀取本地緩存數據
LOAD_DEFAULT: 根據cache-control決定是否從網絡上取數據。
LOAD_CACHE_NORMAL: API level 17中已經廢棄, 從API level 11開始作用同LOAD_DEFAULT模式
LOAD_NO_CACHE: 不使用緩存,只從網絡獲取數據.
LOAD_CACHE_ELSE_NETWORK,只要本地有,無論是否過期,或者no-cache,都使用緩存中的數據。
如:www.taobao.com的cache-control為no-cache,在模式LOAD_DEFAULT下,無論如何都會從網絡上取數據,如果沒有網絡,就會出現錯誤頁面;在LOAD_CACHE_ELSE_NETWORK模式下,無論是否有網絡,只要本地有緩存,都使用緩存。本地沒有緩存時才從網絡上獲取。
www.360.com.cn的cache-control為max-age=60,在兩種模式下都使用本地緩存數據。
總結:根據以上兩種模式,建議緩存策略為,判斷是否有網絡,有的話,使用LOAD_DEFAULT,無網絡時,使用LOAD_CACHE_ELSE_NETWORK。
設置WebView 緩存模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
private
void
initWebView() {
mWebView.getSettings().setJavaScriptEnabled(
true
);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//設置 緩存模式
// 開啟 DOM storage API 功能
mWebView.getSettings().setDomStorageEnabled(
true
);
//開啟 database storage API 功能
mWebView.getSettings().setDatabaseEnabled(
true
);
String cacheDirPath = getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
// String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
Log.i(TAG,
"cacheDirPath="
+cacheDirPath);
//設置數據庫緩存路徑
mWebView.getSettings().setDatabasePath(cacheDirPath);
//設置 Application Caches 緩存目錄
mWebView.getSettings().setAppCachePath(cacheDirPath);
//開啟 Application Caches 功能
mWebView.getSettings().setAppCacheEnabled(
true
);
}
|
清除緩存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/**
* 清除WebView緩存
*/
public
void
clearWebViewCache(){
//清理Webview緩存數據庫
try
{
deleteDatabase(
"webview.db"
);
deleteDatabase(
"webviewCache.db"
);
}
catch
(Exception e) {
e.printStackTrace();
}
//WebView 緩存文件
File appCacheDir =
new
File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
Log.e(TAG,
"appCacheDir path="
+appCacheDir.getAbsolutePath());
File webviewCacheDir =
new
File(getCacheDir().getAbsolutePath()+
"/webviewCache"
);
Log.e(TAG,
"webviewCacheDir path="
+webviewCacheDir.getAbsolutePath());
//刪除webview 緩存目錄
if
(webviewCacheDir.exists()){
deleteFile(webviewCacheDir);
}
//刪除webview 緩存 緩存目錄
if
(appCacheDir.exists()){
deleteFile(appCacheDir);
}
}
|
完整代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package
com.example.webviewtest;
import
java.io.File;
import
android.app.Activity;
import
android.graphics.Bitmap;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.webkit.JsPromptResult;
import
android.webkit.JsResult;
import
android.webkit.WebChromeClient;
import
android.webkit.WebSettings;
import
android.webkit.WebSettings.RenderPriority;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.widget.RelativeLayout;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
private
static
final
String TAG = MainActivity.
class
.getSimpleName();
private
static
final
String APP_CACAHE_DIRNAME =
"/webcache"
;
private
TextView tv_topbar_title;
private
RelativeLayout rl_loading;
private
WebView mWebView;
private
String url;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//url:http://m.dianhua.cn/detail/31ccb426119d3c9eaa794df686c58636121d38bc?apikey=jFaWGVHdFVhekZYWTBWV1ZHSkZOVlJWY&app=com.yulore.yellowsdk_ios&uid=355136051337627
url =
"http://m.dianhua.cn/detail/31ccb426119d3c9eaa794df686c58636121d38bc?apikey=jFaWGVHdFVhekZYWTBWV1ZHSkZOVlJWY&app=com.yulore.yellowsdk_ios&uid=355136051337627"
;
findView();
}
private
void
findView() {
tv_topbar_title = (TextView) findViewById(R.id.tv_topbar_title);
rl_loading = (RelativeLayout) findViewById(R.id.rl_loading);
mWebView = (WebView) findViewById(R.id.mWebView);
initWebView();
mWebView.setWebViewClient(
new
WebViewClient() {
@Override
public
void
onLoadResource(WebView view, String url) {
Log.i(TAG,
"onLoadResource url="
+url);
super
.onLoadResource(view, url);
}
@Override
public
boolean
shouldOverrideUrlLoading(WebView webview, String url) {
Log.i(TAG,
"intercept url="
+url);
webview.loadUrl(url);
return
true
;
}
@Override
public
void
onPageStarted(WebView view, String url, Bitmap favicon) {
Log.e(TAG,
"onPageStarted"
);
rl_loading.setVisibility(View.VISIBLE);
// 顯示加載界面
}
@Override
public
void
onPageFinished(WebView view, String url) {
String title = view.getTitle();
Log.e(TAG,
"onPageFinished WebView title="
+ title);
tv_topbar_title.setText(title);
tv_topbar_title.setVisibility(View.VISIBLE);
rl_loading.setVisibility(View.GONE);
// 隱藏加載界面
}
@Override
public
void
onReceivedError(WebView view,
int
errorCode, String description, String failingUrl) {
rl_loading.setVisibility(View.GONE);
// 隱藏加載界面
Toast.makeText(getApplicationContext(),
""
,
Toast.LENGTH_LONG).show();
}
});
mWebView.setWebChromeClient(
new
WebChromeClient() {
@Override
public
boolean
onJsAlert(WebView view, String url, String message, JsResult result) {
Log.e(TAG,
"onJsAlert "
+ message);
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
result.confirm();
return
true
;
}
@Override
public
boolean
onJsConfirm(WebView view, String url, String message, JsResult result) {
Log.e(TAG,
"onJsConfirm "
+ message);
return
super
.onJsConfirm(view, url, message, result);
}
@Override
public
boolean
onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
Log.e(TAG,
"onJsPrompt "
+ url);
return
super
.onJsPrompt(view, url, message, defaultValue, result);
}
});
mWebView.loadUrl(url);
}
private
void
initWebView() {
mWebView.getSettings().setJavaScriptEnabled(
true
);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//設置 緩存模式
// 開啟 DOM storage API 功能
mWebView.getSettings().setDomStorageEnabled(
true
);
//開啟 database storage API 功能
mWebView.getSettings().setDatabaseEnabled(
true
);
String cacheDirPath = getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
// String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
Log.i(TAG,
"cacheDirPath="
+cacheDirPath);
//設置數據庫緩存路徑
mWebView.getSettings().setDatabasePath(cacheDirPath);
//設置 Application Caches 緩存目錄
mWebView.getSettings().setAppCachePath(cacheDirPath);
//開啟 Application Caches 功能
mWebView.getSettings().setAppCacheEnabled(
true
);
}
/**
* 清除WebView緩存
*/
public
void
clearWebViewCache(){
//清理Webview緩存數據庫
try
{
deleteDatabase(
"webview.db"
);
deleteDatabase(
"webviewCache.db"
);
}
catch
(Exception e) {
e.printStackTrace();
}
//WebView 緩存文件
File appCacheDir =
new
File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
Log.e(TAG,
"appCacheDir path="
+appCacheDir.getAbsolutePath());
File webviewCacheDir =
new
File(getCacheDir().getAbsolutePath()+
"/webviewCache"
);
Log.e(TAG,
"webviewCacheDir path="
+webviewCacheDir.getAbsolutePath());
//刪除webview 緩存目錄
if
(webviewCacheDir.exists()){
deleteFile(webviewCacheDir);
}
//刪除webview 緩存 緩存目錄
if
(appCacheDir.exists()){
deleteFile(appCacheDir);
}
}
/**
* 遞歸刪除 文件/文件夾
*
* @param file
*/
public
void
deleteFile(File file) {
Log.i(TAG,
"delete file path="
+ file.getAbsolutePath());
if
(file.exists()) {
if
(file.isFile()) {
file.delete();
}
else
if
(file.isDirectory()) {
File files[] = file.listFiles();
for
(
int
i =
0
; i < files.length; i++) {
deleteFile(files[i]);
}
}
file.delete();
}
else
{
Log.e(TAG,
"delete file no exists "
+ file.getAbsolutePath());
}
}
}
|