关于Android之WebView加载网页缓存和清除问题

无独有偶,Android开发中也需要一个网页缓存机制,自然网络上成熟的案例举不胜举,比如:


一、设置缓存:

private static final String APP_CACAHE_DIRNAME = "/webcache";

 private void initWebView() {

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //设置 缓存模式
        // 开启 DOM storage API 功能
        mWebView.getSettings().setDomStorageEnabled(false);
        //开启 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);
    }

效果:浏览过网页后断网,再次打开,依然能看到加载的网页,即缓存机制;

二、清除缓存:

public void clearWebViewCache1(){

    //清理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());
    }
}

效果:再次打开网页查看缓存的网页,应该是不会再加载了,可是,可是,可是,现在的情况是依然能加载,失败,失败,失败;


解决方案:

1、各种搜索,各种测试,就是无法删除缓存数据,无语后开始英文搜索;

2、在某一stackoverflow回复区找到如下解决方案,最终完美破解:

public void clearWebViewCache(){

    File file = this.getApplicationContext().getCacheDir().getAbsoluteFile();
    deleteFile(file);
}


/**
 * 递归删除 文件/文件夹
 *
 * @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());
    }
}

最终效果:离线缓存的网页已没有相关数据了,一个教训,还是去英文资料比较实在高效!


良心APP推荐:IT面试宝典 

猜你喜欢

转载自blog.csdn.net/mapboo/article/details/78490089