98利用反射清理应用程序的缓存以及清理所有应用程序的缓存
来源:互联网 发布:c语言求水仙花数的算法 编辑:程序博客网 时间:2024/05/21 18:49
当使用缓存得到单个应用程序的缓存之后,查看源码,得到清除缓存的方法,同样,这里也需要使用反射区清理,当完成后,提示我们需要添加权限:
<uses-permission android:name="android.permission.DELETE_CACHE_FILES"/>
但是当我们保存的时候,发现这条权限居然报错了,意思是:这个权限是授权给系统程序的,所以我们的程序不能使用清楚缓存的功能,当单击清除缓存的功能的时候,弹出到设置界面的清楚缓存的功能。
清理全部的缓存的思路是:
利用了系统的BUG,当系统内存不足的时候,系统会回收应用程序的缓存,我们可以发送信息给系统,告诉系统我们需要Integer.MAX_VALUE的内存空间。
/** * Free storage by deleting LRU sorted list of cache files across * all applications. If the currently available free storage * on the device is greater than or equal to the requested * free storage, no cache files are cleared. If the currently * available storage on the device is less than the requested * free storage, some or all of the cache files across * all applications are deleted (based on last accessed time) * to increase the free storage space on the device to * the requested value. There is no guarantee that clearing all * the cache files from all applications will clear up * enough storage to achieve the desired value. * @param freeStorageSize The number of bytes of storage to be * freed by the system. Say if freeStorageSize is XX, * and the current free storage is YY, * if XX is less than YY, just return. if not free XX-YY number * of bytes if possible. * @param observer call back used to notify when * the operation is completed * * @hide */ public abstract void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer);
public void cleanAll(View view){///freeStorageAndNotifyMethod[] methods = PackageManager.class.getMethods();for(Method method:methods){if("freeStorageAndNotify".equals(method.getName())){try {method.invoke(pm, Integer.MAX_VALUE, new IPackageDataObserver.Stub() {@Overridepublic void onRemoveCompleted(String packageName,boolean succeeded) throws RemoteException {System.out.println(succeeded);}});} catch (Exception e) {e.printStackTrace();}return;}}}
最后,还要添加权限:
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
0 0
- 98利用反射清理应用程序的缓存以及清理所有应用程序的缓存
- 清理app应用程序的缓存
- 利用反射获得程序的缓存大小、应用程序大小、数据大小,利用反射清除缓存,全部清理
- 清理应用程序缓存
- 清理应用程序缓存
- android 获取其他应用程序的缓存大小以及清理应用缓存
- 获取缓存的大小 以及清理缓存
- ajax的缓存清理
- ajax的缓存清理
- 键盘输入缓存的清理
- Xcode的缓存清理
- 设置界面以及清理缓存的实现
- 获取应用程序缓存及一键清理
- WKWebView的缓存问题以及如何正确清理缓存
- 96利用反射获取缓存和应用程序的大小信息
- showModelDialog窗口的缓存清理
- 缓存与session的清理
- Ehcache缓存的清理问题
- 闲谈中的收获
- Agile Java
- java实现字符串kmp算法
- scanf函数的一些问题
- 2015年第一天
- 98利用反射清理应用程序的缓存以及清理所有应用程序的缓存
- java内存的加载过程
- 数据库创建代码
- Java 7的新特性
- 构建百万级高性能Web网站-4 CentOS中文支持
- ECLIPSE中部署工程报“Undefined exploded archive location”错误的解决方法。
- HDU2544 最短路【Dijkstra算法】
- JAVA生成图片验证码
- spring的IOC和AOP实现的设计模式