Android gets the cache size and cleans up the cache

Source: Internet
Author: User

This article focuses on the introduction of actual combat operations, the theoretical knowledge may not be introduced much, do not spray

Simply say two things about the benefits of caching:

The use of cache, can further greatly alleviate the pressure of data interaction, but also to provide a certain offline browsing. Let me briefly list the applicable environment for cache management:

1. Application of network Services

2. Data updates do not need to be updated in real time, and even a 3-5-minute delay can be implemented with a caching mechanism.

3. Cache expiration Time is acceptable (similar to NetEase's news reading, support offline offline reading)

The benefits of this are:

1. Reduce the pressure on the server

2. Improve client response speed (local data extraction)

3. To a certain extent, support offline browsing (can refer to NetEase's news application, the personal feel that offline reading is very good. )


The following piece of code can be implemented to get the cache size and clean up the cache.

Package Com.anbaoyue.manyiwang.utils;import Java.io.file;import Java.math.bigdecimal;import Android.content.context;import Android.os.environment;public class Mydatacleanmanager {public static String Gettotalcachesize (context context) throws Exception {//context.getexternalcachedir ()--sdcard/android/data/ Your app package name/cache/directory, typically storing temporary cache data Long cacheSize = Getfoldersize (Context.getcachedir ());//context.getexternalfilesdir ()-- > sdcard/android/data/your app's package name/files/directory, usually put some long time to save the data if (Environment.getexternalstoragestate (). Equals ( environment.media_mounted)) {cacheSize + = Getfoldersize (Context.getexternalcachedir ());} return Getformatsize ( cacheSize);} public static void Clearallcache (context context) {Deletedir (Context.getcachedir ()); Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletedir (context.getexternalcachedir ());//The following two sentences clean up the WebView Web cache. But each execution is reported false, I use the Phantom Blue 5.1 system, later found and/data/data/application package directory can not find the database text//folder does not know that the problem is not an individual phone, Context.deletedatabase ("WebView.DB "); Context.deletedatabase ("Webviewcache.db");} }private Static Boolean Deletedir (File dir) {if (dir! = null && dir.isdirectory ()) {string[] children = dir.list () ; for (int i = 0; i < children.length; i++) {Boolean success = Deletedir (New File (dir, children[i]); if (!success) {Retu rn false;}}} return Dir.delete ();} Get file//context.getexternalfilesdir ()-sdcard/android/data/your app's package name/files/directory, usually put some long time to save the data// Context.getexternalcachedir ()-sdcard/android/data/your app package name/cache/directory, generally storing temporary cache data public static long Getfoldersize (file file) throws Exception {long size = 0; try {file[] fileList = File.listfiles (); for (int i = 0; i < Filelist.le Ngth; i++) {//If the file below also has the IF (Filelist[i].isdirectory ()) {size = size + getfoldersize (Filelist[i]);} else {size = size + Filel Ist[i].length (); }}} catch (Exception e) {e.printstacktrace ();} return size; }/** * Formatted unit * @param size */public static String getformatsize (double size) {double kilobyte = size/1024; if (kilobyTe < 1) {return size + "Byte";} double megabyte = kilobyte/1024; if (megabyte < 1) {BigDecimal result1 = new BigDecimal (double.tostring (kilobyte)); return Result1.setscale (2, BIGDECIM Al. ROUND_HALF_UP). Toplainstring () + "KB"; } double gigaByte = megabyte/1024; if (GigaByte < 1) {BigDecimal result2 = new BigDecimal (double.tostring (megabyte)); return Result2.setscale (2, BIGDECIM Al. ROUND_HALF_UP). Toplainstring () + "MB"; } double terabytes = gigabyte/1024; if (terabytes < 1) {BigDecimal RESULT3 = new BigDecimal (double.tostring (GigaByte)); return Result3.setscale (2, Bigdeci Mal. ROUND_HALF_UP). Toplainstring () + "GB"; } BigDecimal RESULT4 = new BigDecimal (terabytes); Return Result4.setscale (2, bigdecimal.round_half_up). Toplainstring () + "TB"; }  }



How to use:

In the Fragment

String datasize = mydatacleanmanager.gettotalcachesize (Getactivity (). Getapplicationcontext ());

In the activity, directly

String datasize = mydatacleanmanager.gettotalcachesize (Getapplicationcontext ());

The above code cleanup has been implemented to clean up all the cache files including the WebView cache

However, the use of WebView requires manual opening: about WebView cache processing I wrote a special article: http://blog.csdn.net/wei_chong_chong/article/details/52189011


Reference: http://blog.csdn.net/zhuanshenweiliu/article/details/31744673



Android gets the cache size and cleans up the 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.