Skip to content

Instantly share code, notes, and snippets.

@johngorithm
Last active March 27, 2019 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johngorithm/cf07d7c95069db46404135d77c47337d to your computer and use it in GitHub Desktop.
Save johngorithm/cf07d7c95069db46404135d77c47337d to your computer and use it in GitHub Desktop.
import android.content.Context;
import java.io.File;
public class CacheManager {
/**
* Private Constructor to make this a Utility class.
*/
private CacheManager() {
// Private constructor
}
/**
* Deletes app's cache.
* @param context app context
*/
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) { e.printStackTrace();}
}
/**
* Does the actual deletion of our app's cache.
* @param dir cache directory
* @return boolean value of whether operation was successful or not.
*/
private 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;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment