To start, create the CacheCleaner class (CacheCleaner.java file) within the package "org.apache.cordova.plugins"
package org.apache.cordova.plugins;
import java.io.File;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import android.util.Log;
import android.widget.Toast;
public class CacheCleaner extends CordovaPlugin {
public static final String LOG_PROV = "PhoneGapLog";
public static final String LOG_NAME = "CacheCleaner Plugin";
@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
if (action.equals("del")) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
final File dir = cordova.getActivity().getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
showToast("Cache is deleted.","short");
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_PROV, LOG_NAME + ": Error: " + PluginResult.Status.ERROR);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
}
}
});
return true;
} else {
Log.e(LOG_PROV, LOG_NAME + ": Error: " + PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
return false;
}
}
public static boolean deleteDir(final File dir) {
if (dir != null && dir.isDirectory()) {
final String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
final boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
} else if (dir != null && dir.isFile()) {
dir.delete();
}
return true;
}
private void showToast(final String message, final String duration) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(duration.equals("long")) {
Toast.makeText(cordova.getActivity(), message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(cordova.getActivity(), message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
Next, create a javascript file named "cachecleaner.js"
cordova.define("cordova/plugin/cachecleaner", function (require, exports, module) {
var exec = require("cordova/exec");
module.exports = {
del: function (win, fail) {
exec(win, fail, "CacheCleaner", "del", []);
}
};
});
Be sure to add the following line to your "config.xml" file within the res folder of your Android project.
<feature name="Share"><param name="android-package" value="org.apache.cordova.plugins.CacheCleaner" /></feature>
Finally, use the provided code in your HTML file to clear the cache when a button is clicked.
Here's the function to use:
$(document).on('click','.abs',function(){
var cachecleaner = cordova.require("cordova/plugin/cachecleaner");
cachecleaner.del(
function () {
console.log("PhoneGap Plugin: CacheCleaner: callback success");
window.location.href = "index.html";
},
function () {
console.log("PhoneGap Plugin: CacheCleaner: callback error");
}
);