I used this code to clear installed applications cached data, and it works perfectly on rooted device:
PackageManager pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorage")) {
// Found the method I want to use
try {
long desiredFreeStorage = 8 * 1024; // Request for 8GB of free space
m.invoke(pm, desiredFreeStorage , null);
Toast.makeText(this, "Cache Cleared!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
txtView.setText(e.getMessage());
}
break;
}
}
But this code works with installed applications only, not for system installed apps Like (Gallery), i want to clear cache for a gallery app programmatically, like this action: Clear Gallery Cache