导航:首页 > 文件教程 > androidwebview缓存文件

androidwebview缓存文件

发布时间:2023-05-31 18:20:39

1. android webview加载url怎么缓存

当我们加载Html时候,会在我们data/应用package下生成database与cache两个文件夹:

我们请求的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。

java">设置WebView缓存模式

privatevoidinitWebView(){

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存模式
//开启DOMstorageAPI功能
mWebView.getSettings().setDomStorageEnabled(true);
//开启databasestorageAPI功能
mWebView.getSettings().setDatabaseEnabled(true);
StringcacheDirPath=getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
//StringcacheDirPath=getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
Log.i(TAG,"cacheDirPath="+cacheDirPath);
//设置数据库缓存路径
mWebView.getSettings().setDatabasePath(cacheDirPath);
//设置ApplicationCaches缓存目录
mWebView.getSettings().setAppCachePath(cacheDirPath);
//开启ApplicationCaches功能
mWebView.getSettings().setAppCacheEnabled(true);
}
清除缓存

/**
和祥*清除WebView缓存
*/
publicvoidclearWebViewCache(){

//清理Webview缓存数据库
try{
deleteDatabase("webview.db");
春睁deleteDatabase("webviewCache.db");
}catch(Exceptione){
e.printStackTrace();
}

//WebView缓存文件
FileappCacheDir=newFile(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
Log.e(TAG,"appCacheDirpath="+appCacheDir.getAbsolutePath());

FilewebviewCacheDir=newFile(getCacheDir().getAbsolutePath()+"/webviewCache");
Log.e(TAG,"webviewCacheDirpath="+webviewCacheDir.getAbsolutePath());

//删除webview缓存目录
if(webviewCacheDir.exists()){
deleteFile(webviewCacheDir);
}
//删除webview缓存缓存目录
if(appCacheDir.exists()){
deleteFile(appCacheDir);
}
}

2. android开发,用webview打开本地html网页时,怎么清除缓存

在oncreate 加世正知上清纯
CookieSyncManager.createInstance(this);

CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().removeSessionCookie();
清除搜消 webkit cookie

3. android的webview为js提供localstorage后怎样把缓存文件取出来

1
2
3
4
5
6
7
8
9

mainWebView = (WebView)this.findViewById(R.id.mainWebView);
WebSettings settings = mainWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setDatabaseEnabled(true);
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabasePath(dir);
settings.setDomStorageEnabled(true);
settings.setGeolocationEnabled(true);

4. 如何读写Android的WebView缓存文件

你这个webView不是键凳放在一个新的activity·里面的么?你点击按钮的操作应该是跳转到一个新的activity,然后这个activity里面的布局是你要用到的weiView,这样当稿让旅点击返滑州回键的时候就可以返回到你原来的activity了。只需要这样改 public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; }else{ finish(); } return super.onKeyDown(keyCode, event); }

5. android webview为什么无法缓存

androd webview是支持缓存的,看看你的开关有没有打开族吵:
WebView中存在着两种缓存:网页数据缓存(存储打开过的页面及资源)、H5缓存(即AppCache)。

一、网页缓存
1、缓存构成
/data/桥仔data/package_name/cache/
/data/data/package_name/database/webview.db

/data/data/package_name/database/webviewCache.db

WebView缓存文件结构如下图所兆消侍示

6. 如何读写Android的WebView缓存文件

代码如下: android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"旁饥> android:layout_height="wrap_content" android:id="@+id/webView"/> android:layout_height="wrap_content" android:layout_gravity="兄逗center_horizontal" android:text="从缓存读取羡启卖图片" android:id="

7. android开发,用webview打开本地html网页时,怎么清除缓存

/**
* 清除WebView缓存

*/
public void clearWebViewCache() {

/**清理Webview缓存数据库,缓存文耐庆清件由程序自动生成

* /data/data/package_name/database/webview.db

* /data/data/package_name/database/webviewCache.db

**/
try {
//因为他们昌前都差液是文件,所以可以用io方式删除,具体方法可以自己写
deleteDatabase("webview.db");
deleteDatabase("webviewCache.db");

} catch (Exception e) {

e.printStackTrace();
}

//WebView 缓存文件
File webviewCacheDir = new File(APP_CACAHE_DIRNAME);

//删除webview 缓存目录

if (webviewCacheDir.exists()) {

//具体的方法自己写

deleteFile(webviewCacheDir);
}
}

8. android中 如何清理webview缓存

一、清除cookie

public static void clearCookies(Context context) {
// Edge case: an illegal state exception is thrown if an instance of
// CookieSyncManager has not be created. CookieSyncManager is normally
// created by a WebKit view, but this might happen if you start the
// app, restore saved state, and click logout before running a UI
// dialog in a WebView -- in which case the app crashes
@SuppressWarnings("unused")
CookieSyncManager cookieSyncMngr =
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}

这是facebook sdk的源码,我不知道第一句到底起了什么作用察禅?

二、清除webview缓存,查看root过的手机data下的文件,会发现有这个东西:webview命名的东西

删除保存于手机上的缓存.

// clear the cache before time numDays
private int clearCacheFolder(File dir, long numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
try {
for (File child:dir.listFiles()) {
if (child.isDirectory()) {
deletedFiles += clearCacheFolder(child, numDays);
}
if (child.lastModified() < numDays) {
if (child.delete()) {
deletedFiles++;
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
return deletedFiles;
}

打开关闭使用缓存
//优先使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

//不使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

在退出应用的时候加上如下败碰尘代码
File file = CacheManager.getCacheFileBaseDir();
if (file != null && file.exists() && file.isDirectory()) {
for (File item : file.listFiles()) {
item.delete();
}
file.delete();
}

context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");

发现这个问题,一个朋友在iteye上问的:

Android的CookieManager只提供了removeAllCookies方法,用来删除所吵宏有的cookie,有什么办法只删除和特定url关联的cookie呢?本来打算使用setCookie(url, value)将指定url关联的cookie设为空串,但试了一下发现这个方法只是在已有的基础上继续添加cookie,并不能重置已有的cookie。

有朋友给打答案:

/**
* 同步一下cookie
*/
public static void synCookies(Context context, String url) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();//移除
cookieManager.setCookie(url, cookies);//指定要修改的cookies
CookieSyncManager.getInstance().sync();
}

9. android开发,用webview打开本地html网页时,怎么清除缓存

你在加载本地的url前加上这段缓碧瞎:
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//关闭webview中缓存,也就是不将打开过的页慧物面写入扰空内存中

10. android webview 怎么使用本地缓存

android
webview使段州用本地缓存的话,思路如下:
定义一个离线下载的服务Service
启动后台服务Service来执行异步下载
存储到本地数据库中
每一次加载url之前,先判断数据库是否存在缓存内容
5.如果存在缓存,优先加载本地缓存,如知燃滑搭腊果不存在,才执行联网请求

阅读全文

与androidwebview缓存文件相关的资料

热点内容
苹果12原装数据线怎么感觉有点硬 浏览:764
js获取div中的图片不显示不出来 浏览:291
什么网站有首映动漫 浏览:461
淘宝网络电话叫什么 浏览:231
编程要读哪些书 浏览:134
如何在手机上新建文件夹里添文件 浏览:292
先锋w10刷安卓系统 浏览:787
java设置过期日期 浏览:114
新版本抖音怎么看我的数据比例 浏览:946
什么是3G网络3G的发展史 浏览:269
如何使用ps把图片的文件大小弄小 浏览:880
安卓系统根目录文件夹 浏览:900
手表怎么设置蜂窝网络 浏览:51
旧爱勾搭app还有吗 浏览:141
日外语言编程软件哪个好 浏览:950
小论文发表了但是数据错误怎么办 浏览:952
注册表禁止启动程序运行 浏览:705
网络优化总体流程图 浏览:735
前端程序员简历模板 浏览:706
蜂巢积木编程机器人怎么样 浏览:561

友情链接