android - 如何检测android中webview是否存在缓存?

标签 android webview android-webview android-websettings

在我的 android 应用程序中需要检测缓存是否存在。目前,如果互联网可用,我正在加载网页。如果互联网不可用,请使用以下代码从缓存中加载页面。

if (networkInfo.isConnected()) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
        } else {
            // loading offline
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }

在上面的代码中,我需要为互联网不可用和缓存也不可用再添加一个条件,然后我需要向用户显示消息。因此计划识别任何缓存可用于 webview。但是无法找到确定缓存是否可用的方法。

有以下想法但不知道如何实现我的目标。 1.找一个weview或者webviewsettings的方法返回isCacheAvailable之类的方法。没找到。 2.找出webview缓存文件的确切路径。这样我就可以检查文件大小以确定缓存是否可用。但是没有找到获取 webview 缓存目录的确切路径的方法。

请帮我找出一种方法来识别 webview 缓存是否存在。

最佳答案

webview缓存的路径是getApplicationContext().getCacheDir()

所以方法可以是

private boolean isCacheAvailable(){
    File dir  = getApplicationContext().getCacheDir();
    if (dir.exists())
        return dir.listFiles().length > 0;
    else
        return false;
};

关于android - 如何检测android中webview是否存在缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932917/

相关文章:

android - 如何检查从 android 中的广播接收器接收到的操作类型?

Java Picasa API 目录列表不起作用

java - 语法错误,插入 ";"以完成 FieldDeclaration

android - Android中的TabHost

android - 某些网站在 WebView 中加载为空白,而其他网站则运行良好 - 如何修复?

android - 有没有办法在cordova运行应用程序的设备功能上启动谷歌设备检查

android - 如何识别当前 fragment ?

java - Android 中的文本上划线

android - 分享失败,请重试(仅限whatsapp)

android - Uncaught ReferenceError : getElementById is not defined at null:1