Public classDatacleanhelp {/*** * Clear this app internal cache (/data/data/com.xxx.xxx/cache) * * * *@paramContext*/ Public Static voidCleaninternalcache (Context context) {Deletedir (Context.getcachedir ()); } /*** * Clear all databases of the application (/data/data/com.xxx.xxx/databases) * * * *@paramContext*/ Public Static voidcleandatabases (Context context) {Deletedir (NewFile ("/data/data/" + context.getpackagename () + "/databases")); } /*** * Clear this app sharedpreference (/data/data/com.xxx.xxx/shared_prefs) * * *@paramContext*/ Public Static voidcleansharedpreference (Context context) {Deletedir (NewFile ("/data/data/" + context.getpackagename () + "/shared_prefs")); } /*** * Clear the application database by name * * *@paramContext *@paramDbName*/ Public Static voidCleandatabasebyname (Context context, String DbName) {context.deletedatabase (dbName); } /*** * Clear the contents of/data/data/com.xxx.xxx/files * * * *@paramContext*/ Public Static voidCleanfiles (Context context) {Deletedir (Context.getfilesdir ()); } /*** * Clear the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * *@paramContext*/ Public Static voidCleanexternalcache (Context context) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletedir (cont Ext.getexternalcachedir ()); } } /*** * Clear the file under the custom path, use caution, please do not delete it by mistake. And only supports file deletion in directory * * * *@paramFilePath*/ Public Static voidCleancustomcache (String filePath) {Deletedir (NewFile (FilePath)); } /*** * Clear all data for this app * * *@paramContext *@paramfilepath*/ Public Static voidCleanapplicationdata (context context, String ... filepath) {Cleaninternalcache (context); Cleanexternalcache (context); Cleandatabases (context);//cleansharedpreference (context);cleanfiles (context); Cleancustomcache (Pathutil.getinstance (). Getvoicepath (). toString ()); if(filepath = =NULL) { return; } for(String filepath:filepath) {cleancustomcache (FilePath); } } Public StaticString getapplicationdatasize (context context) {LongSize = 0; Size+=getfoldersize (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {size+=getfoldersize (Context.getexternalcachedir ()); } size+ = Getfoldersize (NewFile ("/data/data/" + context.getpackagename () + "/databases")); Size+=getfoldersize (Context.getfilesdir ()); Size+=getfoldersize (Pathutil.getinstance (). Getvoicepath ()); String sizeformated=getformatsize (size); returnsizeformated; } /*** * Delete method will only delete the file under a folder, if the incoming directory is a file, will not do processing * * *// *@paramDirectory*/ /*private static void Deletefilesbydirectory (File directory) {if (directory! = null && directory.exists ( ) && Directory.isdirectory ()) {for (File item:directory.listFiles ()) {item.delete (); } } }*/ Private Static BooleanDeletedir (File dir) {if(dir! =NULL&&dir.isdirectory ()) {string[] Children=dir.list (); for(inti = 0; i < children.length; i++) { BooleanSuccess = Deletedir (NewFile (dir, children[i])); if(!success) { return false; } } } returnDir.delete (); } //Get File//Context.getexternalfilesdir ()-sdcard/android/data/your app's package name/files/directory, usually put some long-time saved data//Context.getexternalcachedir ()-sdcard/android/data/your app package name/cache/directory, typically storing temporary cache data Public Static Longgetfoldersize (file file) {LongSize = 0; Try{file[] fileList=File.listfiles (); for(inti = 0; i < filelist.length; i++) { //If there is a file below if(Filelist[i].isdirectory ()) {size= size +getfoldersize (Filelist[i]); } Else{size= size +filelist[i].length (); } } } Catch(Exception e) {e.printstacktrace (); } returnsize; } /*** Delete files and directories in the specified directory * *@paramFilePath *@paramDeletethispath *@return */ Public Static voidDeletefolderfile (String FilePath,BooleanDeletethispath) { if(!Textutils.isempty (FilePath)) { Try{File File=NewFile (FilePath); if(File.isdirectory ()) {//If there is a file belowFile files[] =File.listfiles (); for(inti = 0; i < files.length; i++) {deletefolderfile (Files[i].getabsolutepath (),true); } } if(Deletethispath) {if(!file.isdirectory ()) {//if it is a file, deleteFile.delete (); } Else{//Catalogue if(File.listfiles (). Length = = 0) {//there is no file or directory under the directory, deleteFile.delete (); } } } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } /*** Format Unit * *@paramSize *@return */ Public StaticString Getformatsize (Doublesize) { Doublekilobyte = size/1024; if(Kilobyte < 1) { returnSize + "Byte"; } Doublemegabyte = kilobyte/1024; if(Megabyte < 1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); returnResult1.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "KB"; } DoubleGigaByte = megabyte/1024; if(GigaByte < 1) {BigDecimal result2=NewBigDecimal (double.tostring (megabyte)); returnResult2.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "MB"; } Doubleterabytes = gigabyte/1024; if(Terabytes < 1) {BigDecimal RESULT3=NewBigDecimal (double.tostring (gigaByte)); returnResult3.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "GB"; } BigDecimal RESULT4=NewBigDecimal (terabytes); returnResult4.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "TB"; } Public StaticString getcachesize (file file)throwsException {returngetformatsize (getfoldersize (file)); }}
Calculate cache size and Clear cache