当前位置:网站首页>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
边栏推荐
- Explore how @ modelandview can forward data and pages through the source code
- Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
- 基于BottomNavigationView实现底部导航栏
- Problems related to Prometheus cortex using block storage
- SQL中 with函数的详解与用法
- Error alarm of Postgres master-slave replication delay monitoring
- Abnormal record-21
- 个人博客网站搭建
- How does VirtualBox modify the IP network segment assigned to the virtual machine in the "network address translation (NAT)" network mode
- Abnormal record-16
猜你喜欢
随机推荐
Prometheus alarm record persistence (historical alarm saving and Statistics)
sys.dbms_scheduler.create_job创建定时任务(功能更强大丰富)
实习做了啥
MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
oracle undo使用率高问题处理
Tiny4412 HDMI display
同时解决高度塌陷和外边距重叠问题
谷歌AdMob广告学习
Exception record-9
常用于融合去重的窗口函数row_number
oracle库恢复数据
Abnormal record-19
基于BottomNavigationView实现底部导航栏
oracle存储过程中is和as区别
发布自定义插件到本地服务器
org.xml.sax.SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type.2.4.a: 发现了以元素 ‘b
Oracle redo log产生量大的查找思路与案例
Abnormal record-10
Abnormal record-20
Android-Room数据库快速上手









