PackageCom.jianong.xmtt.util;ImportAndroid.content.Context;Importandroid.os.Environment;ImportJava.io.File;ImportJava.math.BigDecimal;/*** Created by Hgl on 2016/6/23.*/ Public classDatacleanmanager {/*** Get Cache Size *@paramContext *@return * @throwsException*/ Public StaticString gettotalcachesize (Context context)throwsException {LongCacheSize =getfoldersize (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {cacheSize+=getfoldersize (Context.getexternalcachedir ()); } returngetformatsize (cacheSize); } /*** Clear Cache *@paramContext*/ Public Static voidClearallcache (Context context) {Deletedir (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletedir (Context.getexternalc Achedir ()); } } 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 Size//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)throwsException {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; } /*** Format Unit *@paramSize *@return */ Public StaticString Getformatsize (Doublesize) { Doublekilobyte = size/1024; if(Kilobyte < 1) {//return size + "Byte"; return"0K"; } Doublemegabyte = kilobyte/1024; if(Megabyte < 1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); returnResult1.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "K"; } DoubleGigaByte = megabyte/1024; if(GigaByte < 1) {BigDecimal result2=NewBigDecimal (double.tostring (megabyte)); returnResult2.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "M"; } 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"; }}
Android gets the cache size and clears the cache