清理应用程序缓存

1、获取应用程序缓存信息

2、清理缓存(也可以不用步骤1,直接清理,步骤1只是为了知道哪些程序有缓存)

1)

Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);   
			List<ApplicationInfo> application = new ArrayList();
			application = pm.getInstalledApplications(0);
			for(int i=0;i<application.size();i++)
			{
				ApplicationInfo app = application.get(i);
				getPackageSizeInfo.invoke(pm, app.packageName,  new IPackageStatsObserver.Stub() 
				{   
					@Override  
					public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException 
					{   
						if(pStats.cacheSize != 0)
						{
							cacheSize += pStats.cacheSize;
							Log.e(TAG, "cacheSize: " + pStats.packageName + "  ;" + pStats.cacheSize); 
						}
					}   
				});
			}


上面的方法 通过反射机制获取应用程序的缓存信息

2)

Method localMethod = pm.getClass().getMethod("freeStorageAndNotify", Long.TYPE,IPackageDataObserver.class);
			Long localLong = Long.valueOf(getEnvironmentSize() - 1L);
			Object[] arrayOfObject = new Object[2];
		    arrayOfObject[0] = localLong;
		    localMethod.invoke(pm,localLong,new IPackageDataObserver.Stub()
		      {
				@Override
				public void onRemoveCompleted(String packageName,
						boolean succeeded) throws RemoteException 
						{
				}});


同样通过反射,来清除缓存

private long getEnvironmentSize()
    {
      File dataFile = Environment.getDataDirectory();
      if (dataFile == null)
        return 0;
      else
      {
        StatFs localStatFs = new StatFs(dataFile.getPath());
        long l2 = localStatFs.getBlockSize();
        return localStatFs.getBlockCount() * l2;
      }
    }

需要用到3个AIDL:
IPackageDataObserver.aidl 

IPackageStatsObserver.aidl

PackageStats.aidl

你可能感兴趣的:(String,object,File,null,application)