Android gets the cache size and clears the cache

Source: Internet
Author: User

 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.