清除APK缓存和获取APK的数据大小

来源:互联网 发布:ipad pro淘宝 编辑:程序博客网 时间:2024/05/21 09:29

现在诸多APK都提供了清除自己缓存的按钮,APK清除自己缓存的功能和系统应用程序管理里面清除缓存不一样。在APK里清除缓存可以清除APKCache目录、外部Cahce目录以及自己定义存储目,而应用程序管理里面清除缓存是调用了系统提供的远程接口,这个远程接口去清除APK系统目录下的缓存,这个清除的更干净些,当然这个需要系统权限,所以第三方APK只能戛然止步。

如果有系统签名文件,清除APK系统缓存可以实现远程接口IPackageDataObserver.aidl来做,别忘了添加android.permission.CLEAR_APP_USER_DATA权限,并且只能在源码下才能编译通过;如果没有系统签名文件,那么就只能手动来清除能清除的文件了。

所以,在自己的应用程序中,可以这样来做。首先,要清楚自己的应用程序在哪些目录中存放了文件,然后一个个去清除它们即可。

cache目录、外部cache目录、自定义存储目录,这是一般情况下能找到并且是APK存放文件的目录,只要清除它们下面的子目录和文件即可。

示意代码:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 清除app的缓存,由于权限问题,没有办法清除app在系统中的所有缓存,只能清除cache目录和app在外部存储存放临时文件的目录( 
  3.      * 或许是自己定义的目录) 
  4.      */  
  5.     public void clearAPPCache(ClearCacheCallBack callback) {  
  6.         try {  
  7.             // 1、清除APP cache目录下的内容  
  8.             File appCacheDir = this.getCacheDir();  
  9.             delFolder(appCacheDir.getAbsolutePath());  
  10.             if (Environment.MEDIA_MOUNTED.equals(Environment  
  11.                     .getExternalStorageState())  
  12.                     || !Environment.isExternalStorageRemovable()) {  
  13.                 // 2、清除APP外部cache目录下的内容  
  14.                 File appExCacheDir = this.getExternalCacheDir();  
  15.                 if (appExCacheDir != null) {  
  16.                     delFolder(appExCacheDir.getAbsolutePath());  
  17.                 }  
  18.                 // 3、清除APP在外部自定义的缓存目录  
  19.                 File appExDir = new File(MyApp.EX_DIR);  
  20.                 if (appExDir != null) {  
  21.                     delFolder(appExDir.getAbsolutePath());  
  22.                 }  
  23.             }  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.             callback.clearResult(0);  
  27.         }  
  28.         callback.clearResult(1);  
  29.     }  
  30.   
  31.     interface ClearCacheCallBack {  
  32.         public void clearResult(int resultCode);  
  33.     }  
  34.   
  35.     public static void delFolder(String folderPath) {  
  36.         try {  
  37.             delAllFile(folderPath); // 删除完里面所有内容  
  38.             String filePath = folderPath;  
  39.             filePath = filePath.toString();  
  40.             java.io.File myFilePath = new java.io.File(filePath);  
  41.             myFilePath.delete(); // 删除空文件夹  
  42.         } catch (Exception e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.   
  47.     public static boolean delAllFile(String path) {  
  48.         boolean flag = false;  
  49.         File file = new File(path);  
  50.         if (!file.exists()) {  
  51.             return flag;  
  52.         }  
  53.         if (!file.isDirectory()) {  
  54.             return flag;  
  55.         }  
  56.         String[] tempList = file.list();  
  57.         File temp = null;  
  58.         for (int i = 0; i < tempList.length; i++) {  
  59.             if (path.endsWith(File.separator)) {  
  60.                 temp = new File(path + tempList[i]);  
  61.             } else {  
  62.                 temp = new File(path + File.separator + tempList[i]);  
  63.             }  
  64.             if (temp.isFile()) {  
  65.                 temp.delete();  
  66.             }  
  67.             if (temp.isDirectory()) {  
  68.                 delAllFile(path + File.separator + tempList[i]);// 先删除文件夹里面的文件  
  69.                 delFolder(path + File.separator + tempList[i]);// 再删除空文件夹  
  70.                 flag = true;  
  71.             }  
  72.         }  
  73.         return flag;  
  74.     }  

另外获取APK的数据大小,可以通过远程接口IPackageStatsObserver.aidl来获取;

首先实现接口:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 通过远程调用接口来获取应用程序的各种大小 
  3.      */  
  4.     public class PkgSizeObserver extends IPackageStatsObserver.Stub {  
  5.         @Override  
  6.         public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)  
  7.                 throws RemoteException {  
  8.             long cachesize; // 缓存大小  
  9.             long datasize; // 数据大小  
  10.             long codesize; // 应用程序大小  
  11.             long totalsize; // 总大小  
  12.             synchronized (Integer.class) {  
  13.                 cachesize = pStats.cacheSize; // 缓存大小  
  14.                 datasize = pStats.dataSize; // 数据大小  
  15.                 codesize = pStats.codeSize; // 应用程序大小  
  16.                 totalsize = cachesize + datasize + codesize;  
  17.                 MyApp.Log("cachesize = " + formateFileSize(cachesize)  
  18.                         + " ,datasize = " + formateFileSize(datasize)  
  19.                         + " ,codesize = " + formateFileSize(codesize)  
  20.                         + " ,totalsize = " + formateFileSize(totalsize));  
  21.             }  
  22.         }  
  23.     }  
其次,通过反射机制获取pm里面的具体方法:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. PackageManager pm = getPackageManager();  
  2.             try {  
  3.                 Method getPackageSizeInfo = pm.getClass().getDeclaredMethod(  
  4.                         "getPackageSizeInfo", String.class,  
  5.                         IPackageStatsObserver.class);  
  6.                 getPackageSizeInfo.invoke(pm, this.getPackageName().toString(),new PkgSizeObserver());  
  7.             } catch (Exception ex) {  
  8.                 ex.printStackTrace();  
  9.             }  

0 0
原创粉丝点击