Android webview 中的缓存
Posted
技术标签:
【中文标题】Android webview 中的缓存【英文标题】:Caching in Android webview 【发布时间】:2012-02-26 02:07:43 【问题描述】:在android webview中加载移动网页和非移动网页哪个更快;加载缓存还是根本不加载?
什么是推荐的加载样式?
现在,当我在所有非移动网站上不加载缓存时,加载速度比在本机浏览器中加载时慢得多。
【问题讨论】:
【参考方案1】:不要使用这些:
viewer.getSettings().setAppCacheMaxSize(1024*1024*8);
viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
viewer.getSettings().setAppCacheEnabled(true);
这些与默认的 webview 内部缓存无关。 Appcache 是一项完全不同的功能,意味着您可以在没有互联网连接的情况下运行网站。它效果不佳,您可能不想使用它。
设置这个:viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT)
就足够了。
【讨论】:
您能否通过指向一些描述 AppCache 的文档的链接来支持这一点?鉴于LOAD_DEFAULT
中包含“默认”一词,除非您在其他地方明确为它设置了不同的值,否则您答案中的最后一行肯定没有任何作用?
setAppCacheMaxSize 已从 api 18 中弃用。developer.android.com/reference/android/webkit/…
@ed-hinchliffe 很难从 Google 本身找到关于此的好的文档,但他们确实在 Google Chrome WebView 上这篇文章的“脱机工作”部分参考了此文档 docs.webplatform.org/wiki/tutorials/appcache_beginner: developer.chrome.com/multidevice/android/overview。你也是正确的,他在答案中的最后一行什么也没做。
默认值为 @link #LOAD_DEFAULT
您能解释一下为什么我们不应该使用这些函数吗?我没有在 Android 文档中找到警告。【参考方案2】:
当然,缓存的方法应该更快。这就是缓存存在的确切原因。
但你应该没问题,除非你专门禁用 webview 的缓存。如果你不这样做 - 它会默认使用缓存。
【讨论】:
谢谢。这是要缓存的智能样式还是有问题? viewer.getSettings().setAppCacheMaxSize(1024*1024*8); viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache"); viewer.getSettings().setAppCacheEnabled(true); viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 是的,我看不出有什么问题(我假设您在实际代码中输入了真实的包名:))。如果有疑问,您可以调整大小并进行一些测试,看看哪个最适合您正在加载的内容类型。如果页面很大并且您计划加载许多不同的页面 - 可能需要比 1Mb 更大的缓存。如果它们主要是文本,并且您的应用只需要显示其中的一小部分 - 您可能对当前设置没问题。 在使用AppCache之前,看看这个:code.google.com/p/android/issues/detail?id=24180 developer.android.com/reference/android/webkit/… 用于 html 5 的应用程序缓存 api。它已被弃用,并且似乎在 webview 缓存中不起作用,除非在 html 页面上实现:developer.android.com/reference/android/webkit/… 是的,如果它是一个片段,不要硬编码使用它:getActivity().getApplicationContext().getCacheDir().getAbsolutePath()
【参考方案3】:
/*
public abstract void setAppCacheEnabled (boolean flag)
Sets whether the Application Caches API should be enabled. The default is false.
Note that in order for the Application Caches API to be enabled, a valid database
path must also be supplied to setAppCachePath(String).
Parameters
flag : true if the WebView should enable Application Caches
*/
// Enable the caching for web view
mWebView.getSettings().setAppCacheEnabled(true);
/*
public abstract void setAppCachePath (String appCachePath)
Sets the path to the Application Caches files. In order for the Application Caches
API to be enabled, this method must be called with a path to which the application
can write. This method should only be called once: repeated calls are ignored.
Parameters
appCachePath : a String path to the directory containing Application Caches files.
*/
/*
public abstract File getCacheDir ()
Returns the absolute path to the application specific cache directory on the
filesystem. These files will be ones that get deleted first when the device runs
low on storage. There is no guarantee when these files will be deleted.
Note: you should not rely on the system deleting these files for you; you should
always have a reasonable maximum, such as 1 MB, for the amount of space you consume
with cache files, and prune those files when exceeding that space.
The returned path may change over time if the calling app is moved to an adopted
storage device, so only relative paths should be persisted.
Apps require no extra permissions to read or write to the returned path,
since this path lives in their private storage.
Returns
The path of the directory holding application cache files.
*/
/*
public String getPath ()
Returns the path of this file.
*/
// Specify the app cache path
mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());
/*
public abstract void setCacheMode (int mode)
Overrides the way the cache is used. The way the cache is used is based on the
navigation type. For a normal page load, the cache is checked and content is
re-validated as needed. When navigating back, content is not re-validated, instead
the content is just retrieved from the cache. This method allows the client to
override this behavior by specifying one of
LOAD_DEFAULT,
LOAD_CACHE_ELSE_NETWORK,
LOAD_NO_CACHE or
LOAD_CACHE_ONLY.
The default value is LOAD_DEFAULT.
Parameters
mode : the mode to use
*/
/*
public static final int LOAD_DEFAULT
Default cache usage mode. If the navigation type doesn't impose any specific
behavior, use cached resources when they are available and not expired, otherwise
load resources from the network. Use with setCacheMode(int).
Constant Value: -1 (0xffffffff)
*/
/*
public static final int LOAD_CACHE_ELSE_NETWORK
Use cached resources when they are available, even if they have expired. Otherwise
load resources from the network. Use with setCacheMode(int).
Constant Value: 1 (0x00000001)
*/
/*
public static final int LOAD_NO_CACHE
Don't use the cache, load from the network. Use with setCacheMode(int).
Constant Value: 2 (0x00000002)
*/
/*
public static final int LOAD_CACHE_ONLY
Don't use the network, load from the cache. Use with setCacheMode(int).
Constant Value: 3 (0x00000003)
*/
// Set the cache mode
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
【讨论】:
以上是关于Android webview 中的缓存的主要内容,如果未能解决你的问题,请参考以下文章