当前位置:网站首页>Android清除应用缓存
Android清除应用缓存
2022-04-23 06:08:00 【lebulangzhen】
第一种
使用ActivityManager中的clearApplicationUserData方法,代码如下:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.clearApplicationUserData();
这种方式的问题就是不止清除cache,连带应用的data也删除了。如下图所示:
第二种
使用PackageManager中的deleteApplicationCacheFiles方法,这个API是隐藏的,所以需要用反射来调用,代码如下:
PackageManager packageManager = context.getPackageManager();
Method method;
try {
method = PackageManager.class.getDeclaredMethod("deleteApplicationCacheFiles", String.class, IPackageDataObserver.class);
method.invoke(packageManager, packageName, new ClearUserDataObserver());
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
static class ClearUserDataObserver extends IPackageDataObserver.Stub {
public void onRemoveCompleted(final String packageName, final boolean succeeded) {
Log.i(TAG, "IPackageDataObserver succeeded: " + succeeded);
}
}
这种方式只会清除cache,有需要只清除缓存,可以是用这种方式。IPackageDataObserver需要自己放到项目的aidi文件夹中,如下图所示:
两种方式都需要有系统权限,声明为系统应用并且签系统签名才能调用成功。
版权声明
本文为[lebulangzhen]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lebulangzhen/article/details/114675718
边栏推荐
猜你喜欢
iTOP4412 HDMI显示(4.0.3_r1)
Dolphinscheduler调度spark任务踩坑记录
webView因证书问题显示一片空白
this.getOptions is not a function
Encapsulate a set of project network request framework from 0
oracle表的约束详解
窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
qs. In the stringify interface, the input parameter is converted into a & connected string (with the application / x-www-form-urlencoded request header)
Dolphinscheduler调度sql任务建表时The query did not generate a result set异常解决
Prometheus thanos Quick Guide
随机推荐
利用队列实现栈
Abnormal record-16
oracle生成毫秒级时间戳
Implementation of multi tenant read and write in Prometheus cortex
Prometheus thanos Quick Guide
MySQL笔记5_操作数据
Tiny4412 HDMI display
iTOP4412无法显示开机动画(4.0.3_r1)
Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)
Abnormal record-13
组件化学习
[SM8150][Pixel4]LCD驱动
Oracle redo log产生量大的查找思路与案例
[sm8150] [pixel4] LCD driver
PG SQL截取字符串到指定字符位置
oracle数据库将多个列的查询结果集合并到一行中
Abnormal record-11
从0开始封装一套项目的网络请求框架
oracle杀死正在执行的sql
Android面试计网面经大全【持续更新中。。。】