Main features clear internal/external cache, clear database, clear sharedpreference, purge files and clear custom directories
/* * File Name: Datacleanmanager.java * Description: The main function is to clear the internal/external cache, clear the database, clear the sharedpreference, clear files and clear the custom directory */import java.io.Fi Le;import Android.content.context;import android.os.environment;/** * This app 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 stat IC void Cleansharedpreference (context context) {Deletefilesbydirectory (New File ("/data/data/" + con Text.getpackagename () + "/shared_prefs"); /** * Clear the application database by name * * @param cOntext * @param dbName */public static void Cleandatabasebyname (context context, String DbName) {Context.delet Edatabase (DbName); /** * Clear the contents under/data/data/com.xxx.xxx/files * * @param context */public static void Cleanfiles (context context) { Deletefilesbydirectory (Context.getfilesdir ()); }/** * * Clears the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param * Context */public Stati c void Cleanexternalcache (context context) {if (Environment.getexternalstoragestate (). Equals (EnviR Onment. 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) {Deletefilesbydirecto Ry (New File (FilePath)); /** * Clears all data from 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 will only delete files under a folder, if the incoming directory is a file, will not do processing * * @param directory */private static void Deletefilesby Directory (File directory) {if (directory! = null && directory.exists () && directory.isdirectory ()) {for (File item:directory.listFiles ()) {item.delete (); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android clears local data cache code