Android clear Local data cache code case
Directly on the code:
/* * FileName: datacleanmanager.java * Description Description: Main functions are to clear the internal/external cache, clear the database, Clear sharedpreference, clear files and clear custom directories */ Package com.test.dataclean; Import Java.io.File ; Import android.content.context; Import android.os.environment; /** * This app data Removal 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) { & nbsp; context.deletedatabase (dbName); } /** * Clear the contents of/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 &Nbsp; * * @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 will be deleted. If the incoming DirecTory is a file that 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 clears local data cache code case