Android cache cleanup function

Source: Internet
Author: User

Android cache cleanup function

Application data clearing Manager

DataCleanManager. java is extracted from the Internet, and the name is lost.

Loading a webview to generate a cache. We all know that webview is one of the main reasons for caching.

 

After loading the webview, click the button to query the cache and then clear the cache. Then, you can see that the cache is indeed cleared.

Or, after webview is loaded, click the button to query the cache and then set the app to see if the app's cache is the same. The answer must be the same.

Below is the code

 

DataCleanManager. java

 

Package com. yqy. yqy_cache;/** file name: DataCleanManager. java * Description: The main functions are to clear the internal/external cache, clear the database, clear sharedPreference, clear files, and clear the custom directory */import java. io. file; import java. math. bigDecimal; import android. content. context; import android. OS. environment;/*** data clearing manager for this application */public class DataCleanManager {public static String getTotalCacheSize (Context context) throws Exception {long cacheSize = getFolderSize (context. getCa CheDir (); if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {cacheSize + = getFolderSize (context. getExternalCacheDir ();} return getFormatSize (cacheSize);} // get the file // Context. getExternalFilesDir () --> SDCard/Android/data/the package name/files/directory of your application. Generally, save some long-time data // Context. getExternalCacheDir () --> SDCard/Android/data/your application package name/cache/directory, generally stores temporary cache data public static long getFolderSize (Fil E file) throws Exception {long size = 0; try {File [] fileList = file. listFiles (); for (int I = 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 ();} return size;} public static void clearAllCache (Context context) {deleteDir (context. getC AcheDir (); if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {deleteDir (context. getExternalCacheDir ();} private static boolean deleteDir (File dir) {if (dir = null) {return false;} 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) {return false ;}} return dir. delete ();}/*** formatting unit ** @ param size * @ return */public static String getFormatSize (double size) {double kiloByte = size/1024; if (kiloByte <1) {// return size + Byte; return 0 K;} double megaByte = kiloByte/1024; if (megaByte <1) {BigDecimal result1 = new BigDecimal (Double. toString (kiloByte); return result1.setScale (2, BigDecimal. ROUND_HALF_U P ). toPlainString () + KB;} double gigaByte = megaByte/1024; if (gigaByte <1) {BigDecimal result2 = new BigDecimal (Double. toString (megaByte); return result2.setScale (2, BigDecimal. ROUND_HALF_UP ). toPlainString () + MB;} double teraBytes = gigaByte/1024; if (teraBytes <1) {BigDecimal result3 = new BigDecimal (Double. toString (gigaByte); return result3.setScale (2, BigDecimal. ROUND_HALF_UP ). ToPlainString () + GB;} BigDecimal result4 = new BigDecimal (teraBytes); return result4.setScale (2, BigDecimal. ROUND_HALF_UP ). toPlainString () + TB;}/*** clear the internal cache of the application (/data/com. xxx. xxx/cache) ** @ param context */public static void cleanInternalCache (Context context) {deleteFilesByDirectory (context. getCacheDir ();}/*** clear all databases of the application (/data/com. xxx. xxx/databases) ** @ param context */public st Atic void cleanDatabases (Context context) {deleteFilesByDirectory (new File (/data/+ context. getPackageName () +/databases);}/***** clear the SharedPreference (/data/com. xxx. xxx/shared_prefs) ** @ param * context */public static void cleanSharedPreference (Context context) {deleteFilesByDirectory (new File (/data/+ context. getPackageName () +/shared_prefs);}/*** clear the application database by name ** @ param cont Ext * @ param dbName */public static void cleanDatabaseByName (Context context, String dbName) {context. deleteDatabase (dbName);}/*** clear/data/com. xxx. content in xxx/files ** @ param context */public static void cleanFiles (Context context) {deleteFilesByDirectory (context. getFilesDir ();}/***** clear content in the external cache (/mnt/sdcard/android/data/com. xxx. xxx/cache) ** @ param * context */public static void cleanExte RnalCache (Context context) {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {deleteFilesByDirectory (context. getExternalCacheDir () ;}}/*** clear the files in the custom path. Be careful when using the command. Do not delete the files by mistake. Only files in the directory can be deleted ** @ param filePath */public static void cleanCustomCache (String filePath) {deleteFilesByDirectory (new File (filePath ));} /*** clear all data of this application ** @ param context * @ param filepath */public static void cleanApplicationData (Context context, String... filepath) {cleanInternalCache (context); cleanExternalCache (context); cleanDatabases (context); cleanSharedPreference (context); cleanFiles (context); fo R (String filePath: filepath) {cleanCustomCache (filePath) ;}}/*** the deletion method only deletes files in a folder. If the imported directory is a file, no processing will be performed ** @ param directory */private static void deleteFilesByDirectory (File directory) {if (directory! = Null & directory. exists () & directory. isDirectory () {for (File item: directory. listFiles () {item. delete ();}}}}

MainActivity. java

 

 

Package com. yqy. yqy_cache; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. webkit. webView; import android. widget. button; public class MainActivity extends Activity {private Button btn_clear; private WebView wv; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn_clear = (Button) findViewById (R. id. btn_clear); wv = (WebView) findViewById (R. id. wv); btn_clear.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubtry {// View the cache size Log. e (YQY, DataCleanManager. getTotalCacheSize (MainActivity. this);} catch (Exception e) {e. printStackTrace ();} // clear DataCleanManager. clearAllCache (MainActivity. this); try {// operation Log after clearing. e (YQY, DataCleanManager. getTotalCacheSize (MainActivity. this);} catch (Exception e) {e. printStackTrace () ;}}); wv. loadUrl (http://www.baidu.com) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

 

 

 
  
   
 



 

 

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.