【问题标题】:Is there any way to Clear all Cache files programmatically?有没有办法以编程方式清除所有缓存文件?
【发布时间】:2021-09-21 11:11:03
【问题描述】:

我正在开发一个删除所有程序缓存的程序。这个问题之前被问过很多次,但是解决方案要么在较新版本的 Android 中被删除,要么没有清除 所有 缓存,或者他们需要 root 设备。

我在Android docs 中找到了一个添加到api 30 中的解决方案,应该使用Activity#startActivityForResult(Intent, int) 启动,以便用户知道哪个应用程序正在请求清除缓存。 此操作需要清单中的Manifest.permission.MANAGE_EXTERNAL_STORAGE

我在我的清单中有这个权限,我在我的 OnClick 方法中使用了以下代码:

Intent clearCache = new Intent();
clearCache.setAction(ACTION_CLEAR_APP_CACHE);
startActivity(clearCache);

但它不适用于我的 Android 11 设备。有人知道代码问题吗?

谢谢。

【问题讨论】:

  • “但它在我的 Android 11 设备中不起作用”——您希望它做什么?请注意,您的 Intent 不适用于 ACTION_CLEAR_APP_CACHE

标签: android android-studio caching


【解决方案1】:

这个意图应该使用 Activity#startActivityForResult(Intent, int) 让用户知道 哪个应用请求清除缓存。

检查documentation

使用“startActivityForResult”代替“startActivity”:

//Get cache dialog
int requestCode = 999;
Intent intent = new Intent(StorageManager.ACTION_CLEAR_APP_CACHE);
activity.startActivityForResult(intent, requestCode);

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多