`
shuai1234
  • 浏览: 934348 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

清除应用程序所有缓存

 
阅读更多
  1. /* 
  2.  * 文 件 名:  DataCleanManager.java 
  3.  * 描    述:  主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录 
  4.  */  
  5. package com.test.DataClean;  
  6.   
  7. import java.io.File;  
  8.   
  9. import android.content.Context;  
  10. import android.os.Environment;  
  11.   
  12. /** 
  13.  * 本应用数据清除管理器 
  14.  */  
  15. public class DataCleanManager {  
  16.     /** 
  17.      * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) 
  18.      *  
  19.      * @param context 
  20.      */  
  21.     public static void cleanInternalCache(Context context) {  
  22.         deleteFilesByDirectory(context.getCacheDir());  
  23.     }  
  24.   
  25.     /** 
  26.      * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) 
  27.      *  
  28.      * @param context 
  29.      */  
  30.     public static void cleanDatabases(Context context) {  
  31.         deleteFilesByDirectory(new File("/data/data/"  
  32.                 + context.getPackageName() + "/databases"));  
  33.     }  
  34.   
  35.     /** 
  36.      * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) 
  37.      *  
  38.      * @param context 
  39.      */  
  40.     public static void cleanSharedPreference(Context context) {  
  41.         deleteFilesByDirectory(new File("/data/data/"  
  42.                 + context.getPackageName() + "/shared_prefs"));  
  43.     }  
  44.   
  45.     /** 
  46.      * 按名字清除本应用数据库 
  47.      *  
  48.      * @param context 
  49.      * @param dbName 
  50.      */  
  51.     public static void cleanDatabaseByName(Context context, String dbName) {  
  52.         context.deleteDatabase(dbName);  
  53.     }  
  54.   
  55.     /** 
  56.      * 清除/data/data/com.xxx.xxx/files下的内容 
  57.      *  
  58.      * @param context 
  59.      */  
  60.     public static void cleanFiles(Context context) {  
  61.         deleteFilesByDirectory(context.getFilesDir());  
  62.     }  
  63.   
  64.     /** 
  65.      * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) 
  66.      *  
  67.      * @param context 
  68.      */  
  69.     public static void cleanExternalCache(Context context) {  
  70.         if (Environment.getExternalStorageState().equals(  
  71.                 Environment.MEDIA_MOUNTED)) {  
  72.             deleteFilesByDirectory(context.getExternalCacheDir());  
  73.         }  
  74.     }  
  75.   
  76.     /** 
  77.      * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 
  78.      *  
  79.      * @param filePath 
  80.      */  
  81.     public static void cleanCustomCache(String filePath) {  
  82.         deleteFilesByDirectory(new File(filePath));  
  83.     }  
  84.   
  85.     /** 
  86.      * 清除本应用所有的数据 
  87.      *  
  88.      * @param context 
  89.      * @param filepath 
  90.      */  
  91.     public static void cleanApplicationData(Context context, String... filepath) {  
  92.         cleanInternalCache(context);  
  93.         cleanExternalCache(context);  
  94.         cleanDatabases(context);  
  95.         cleanSharedPreference(context);  
  96.         cleanFiles(context);  
  97.         for (String filePath : filepath) {  
  98.             cleanCustomCache(filePath);  
  99.         }  
  100.     }  
  101.   
  102.     /** 
  103.      * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 
  104.      *  
  105.      * @param directory 
  106.      */  
  107.     private static void deleteFilesByDirectory(File directory) {  
  108.         if (directory != null && directory.exists() && directory.isDirectory()) {  
  109.             for (File item : directory.listFiles()) {  
  110.                 item.delete();  
  111.             }  
  112.         }  
  113.     }  
  114. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics