Android clear Local data cache code case
Directly on the code:
/* * File Name: Datacleanmanager.java * Description: Main function is to clear the inside/out cache, clear the database, clear the sharedpreference, clear files and clear the custom directory */
Package Com.test.DataClean;
Import Java.io.File;
Import Android.content.Context;
Import android.os.Environment;
/** * This application Data Purge Manager */
public class Datacleanmanager {
/** * Clear this app internal cache (/data/data/com.xxx.xxx/cache) * * @param context */
public static void Cleaninternalcache (context context) {
Deletefilesbydirectory (Context.getcachedir ());
}
/** * Clear all databases for this application (/data/data/com.xxx.xxx/databases) * * @param context */
public static void Cleandatabases (context context) {
Deletefilesbydirectory (New File ("/data/data/" + context.getpackagename () + "/databases"));
}
/** * Clear this app sharedpreference (/data/data/com.xxx.xxx/shared_prefs) * * @param context */
public static void Cleansharedpreference (context context) {
Deletefilesbydirectory (New File ("/data/data/" + context.getpackagename () + "/shared_prefs"));
}
/** * Clear the application database by name * * @param context * @param dbName */
public static void Cleandatabasebyname (context context, String DbName) {context.deletedatabase (dbName);
}
/** * Clear the contents of the/data/data/com.xxx.xxx/files * * @param context * *
public static void Cleanfiles (context context) {
Deletefilesbydirectory (Context.getfilesdir ());
}
/** * Clear the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param context */
public static void Cleanexternalcache (context context) {
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
Deletefilesbydirectory (Context.getexternalcachedir ());
}
}
/** * Clear the file under the custom path, use caution, please do not delete it by mistake. And only supports file deletion in directory * * @param filePath */
public static void Cleancustomcache (String filePath) {
Deletefilesbydirectory (New File (FilePath));
}
/** * Clear all data for this app * * @param context * @param filepath */
public static void Cleanapplicationdata (context context, String ... filepath) {
Cleaninternalcache (context);
Cleanexternalcache (context);
Cleandatabases (context);
Cleansharedpreference (context);
Cleanfiles (context);
for (String Filepath:filepath) {
Cleancustomcache (FilePath);
}
}
/** * Delete Method only the files under a folder are deleted, if the incoming directory is a file, it will not be processed * * @param directory */
private static void Deletefilesbydirectory (File directory) {
if (directory! = null && directory.exists () && directory.isdirectory ()) {
For (File item:directory.listFiles ()) {
Item.delete ();
}
}
}
}
Android clear Local data cache code case