Android清除本機資料緩衝代碼案例

來源:互聯網
上載者:User

直接上代碼:

  1. /*  * 文 件 名:  DataCleanManager.java  * 描    述:  主要功能有清除內/外緩衝,清除資料庫,清除sharedPreference,清除files和清除自訂目錄  */  
  2. package com.test.DataClean;    
  3. import java.io.File;   
  4. import android.content.Context;  
  5. import android.os.Environment;   
  6.  /**  * 本應用資料清除管理器  */  
  7.  public class DataCleanManager {    
  8.  /**      * 清除本應用內部緩衝(/data/data/com.xxx.xxx/cache)      *       * @param context      */   
  9.  public static void cleanInternalCache(Context context) {      
  10.  deleteFilesByDirectory(context.getCacheDir());     
  11.  }       
  12.  /**      * 清除本應用所有資料庫(/data/data/com.xxx.xxx/databases)      *       * @param context      */    
  13.  public static void cleanDatabases(Context context) {         
  14.  deleteFilesByDirectory(new File("/data/data/"                 + context.getPackageName() + "/databases"));   
  15.  }       
  16.  /**      * 清除本應用SharedPreference(/data/data/com.xxx.xxx/shared_prefs)      *       * @param context      */    
  17.  public static void cleanSharedPreference(Context context) {       
  18.  deleteFilesByDirectory(new File("/data/data/"                 + context.getPackageName() + "/shared_prefs"));     
  19.  }      
  20.  /**      * 按名字清除本應用程式資料庫      *       * @param context      * @param dbName      */     
  21.  public static void cleanDatabaseByName(Context context, String dbName) {         context.deleteDatabase(dbName);     
  22.  }      
  23.  /**      * 清除/data/data/com.xxx.xxx/files下的內容      *       * @param context      */    
  24.  public static void cleanFiles(Context context) {        
  25.  deleteFilesByDirectory(context.getFilesDir());      
  26.  }       
  27.  /**      * 清除外部cache下的內容(/mnt/sdcard/android/data/com.xxx.xxx/cache)      *       * @param context      */   
  28.  public static void cleanExternalCache(Context context) {      
  29.  if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {        
  30.  deleteFilesByDirectory(context.getExternalCacheDir());        
  31.  }      
  32.  }     
  33.  /**      * 清除自訂路徑下的檔案,使用需小心,請不要誤刪。而且只支援目錄下的檔案刪除      *       * @param filePath      */   
  34.  public static void cleanCustomCache(String filePath) {      
  35.  deleteFilesByDirectory(new File(filePath));     
  36.  }     
  37.  /**      * 清除本應用所有的資料      *       * @param context      * @param filepath      */   
  38.  public static void cleanApplicationData(Context context, String... filepath) {         
  39.  cleanInternalCache(context);        
  40.  cleanExternalCache(context);        
  41.  cleanDatabases(context);         
  42.  cleanSharedPreference(context);        
  43.  cleanFiles(context);          
  44.  for (String filePath : filepath) {         
  45.  cleanCustomCache(filePath);      
  46.  }     
  47.  }      
  48.  /**      * 刪除方法 這裡只會刪除某個檔案夾下的檔案,如果傳入的directory是個檔案,將不做處理      *       * @param directory      */  
  49.  private static void deleteFilesByDirectory(File directory) {      
  50.  if (directory != null && directory.exists() && directory.isDirectory()) {       
  51.  for (File item : directory.listFiles()) {             
  52.  item.delete();             
  53.  }         
  54.  }     
  55.  }  
  56.  }  
  57.   
  58.   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.