问题
I am trying to clear the cache stored in android application which uses cordova webview.
I tried with cordovaWebView.clearCache(true);
Also tried with
public void deleteCache(Context context) {
Log.i("Utility", "deleting Cache");
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
Log.e("Utility", " exception in deleting cookies");
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
} else if (dir != null && dir.isFile()) {
dir.delete(); // delete the file INSIDE the directory
}
Log.i("Utility", "deleting Cache " + dir.delete());
return true;
}
But both didnt work. May I get any solution for this, as in web view user use to login and hence we need to clear the cache when loading the app second time.
回答1:
I'm using the "cordova-plugin-cache-clear" plugin
https://github.com/anrip/cordova-plugin-cache-clear
To use the plugin, simply call window.CacheClear(success, error);
and it cleans the webView cache.
回答2:
Why don't you use the org.felixtioh.phonegap.plugins.cachecleaner plugin
http://plugreg.com/plugin/sagittaros/CacheCleaner
It worked fine for me.
回答3:
Simplest answer to this is
cordovaWebView.clearCache(true);
android.webkit.CookieManager.getInstance().removeAllCookie();
cordovaWebView is the instance of Cordovawebview.
Use both in the event where you need to clear the cookie and cache.
来源:https://stackoverflow.com/questions/26754884/cordova-web-view-cache-clear-in-android