Android Empty Cache

Source: Internet
Author: User

App development often has the ability to calculate cache size and empty the cache, a feature that is common enough to be seen by almost every application, with code to implement this feature:
The steps are:
1. Get the cache path
Obtain long-time saved files, Context.getexternalfilesdir ();
Storage location, sdcard/android/data/the package name of your app/files/
Get temporary cache, Context.getexternalcachedir ();
Storage location, sdcard/android/data/your app package name/cache/
2. Delete Cache

Package Com.smartcontrol.utils;import java.io.file;import java.math.bigdecimal;import android.content.Context; Import android.os.Environment;/** * Clear Cache **/ Public classCleanmessageutil {/** * @param context * @return * @throws Exception * Get current cache*/     Public StaticString gettotalcachesize (context context) throws Exception {LongCacheSize =getfoldersize (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {cacheSize +=getfoldersize (Context.getexternalcachedir ()); }        returngetformatsize (cacheSize); }    /** * @param context * Delete Cache*/     Public Static voidClearallcache (Context context) {Deletedir (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletedir (cont        Ext.getexternalcachedir ()); }    }    Private Staticboolean Deletedir (File dir) {if(dir! =NULL&&dir.isdirectory ()) {string[] Children=dir.list (); intSize =0; if(Children! =NULL) {size=children.length;  for(inti =0; i < size; i++) {Boolean success= Deletedir (NewFile (dir, children[i])); if(!success) {                        return false; }                }            }        }        if(dir = =NULL) {            return true; } Else {            returnDir.delete (); }    }    //Get File//Context.getexternalfilesdir ()-sdcard/android/data/your app's package name/files///Table of contents, usually put some long time to save the data//Context.getexternalcachedir ()--//sdcard/android/data/your App package name/cache/directory, typically storing temporary cache data     Public Static Longgetfoldersize (File file) throws Exception {LongSize =0; Try{file[] fileList=File.listfiles (); intSize2 =0; if(FileList! =NULL) {Size2=filelist.length;  for(inti =0; i < size2; 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; }    /** Formatted unit * Calculates the size of the cache * @param a size * @return*/     Public StaticString Getformatsize (Doublesize) {        Doublekilobyte = size/1024x768; if(Kilobyte <1) {            //return size + "Byte";            return "0K"; }        Doublemegabyte = kilobyte/1024x768; if(Megabyte <1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); returnResult1.setscale (2, bigdecimal.round_half_up). toplainstring ()+"KB"; }        DoubleGigaByte = megabyte/1024x768; if(GigaByte <1) {BigDecimal result2=NewBigDecimal (double.tostring (megabyte)); returnResult2.setscale (2, bigdecimal.round_half_up). toplainstring ()+"MB"; }        Doubleterabytes = gigaByte/1024x768; 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"; }}

The above code is a well-encapsulated class, click Invoke:

Cleanmessageutil.clearallcache (Getapplicationcontext ());

Android Empty Cache

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.