简体   繁体   English

以编程方式清除应用缓存?

[英]Clearing app cache programmatically?

I want to write a utility where the user can select a set of installed apps and clear their data caches ie like the way you can do manually using the built-in Settings->Applictions settings screen with the "Clear cache" button. 我想编写一个实用程序,用户可以在其中选择一组已安装的应用程序并清除其数据缓存,例如您可以使用内置的“设置” - >“应用程序”设置屏幕和“清除缓存”按钮手动执行此操作。

How can I access how much cached data each app has and programmatically clear these caches? 如何访问每个应用程序拥有的缓存数据量并以编程方式清除这些缓存?

The answer given here is false, there are market apps which have a programmatic clear all app cache functionality. 这里给出的答案是错误的,有市场应用程序具有程序清除所有应用程序缓存功能。 Furthermore the following is in the documentation: 此外,文档中还包含以下内容:

public static final String CLEAR_APP_CACHE

Since: API Level 1 in android Allows an application to clear the caches of all installed applications on the device. 从以下版本开始:android中的API Level 1允许应用程序清除设备上所有已安装应用程序的缓存。 Constant Value: 常数值:

 android.permission.CLEAR_APP_CACHE

Note the "all installed applications portion", and the fact that you don't need any root access or special permissions beyond a standard user permission. 请注意“所有已安装的应用程序部分”,以及除标准用户权限之外不需要任何超级用户访问权限或特殊权限的事实。

I post here because I have a requirement on a deliverable to clear all app caches every 24 hours on an in store demo phone and I'm trying to figure out how to use this properly. 我在这里发帖是因为我要求可交付成果每24小时在店内演示电话中清除所有应用程序缓存,并且我正在试图弄清楚如何正确使用它。 I know that part of the solution is getting this permission, I also know how to find all installed applications on the device, and I'm working on being able to actually delete the cache. 我知道解决方案的一部分是获得此权限,我也知道如何在设备上找到所有已安装的应用程序,我正在努力实际删除缓存。 My issue is other app caches read as containing no files (despite having content), I suspect because I don't have read access to make the list Files call. 我的问题是其他应用程序缓存读取为不包含任何文件(尽管有内容),我怀疑因为我没有读取权限使列表文件调用。

Ordinary SDK applications have no rights to access, let alone modify, the caches of other applications, any more than they have a right to hack your files. 普通的SDK应用程序无权访问,更不用说修改其他应用程序的缓存,只不过他们有权​​破解您的文件。

This may be possible on rooted phones with your application running as root, in which case you will have to manually construct the paths based upon the apps' package names. 这可能适用于以root用户身份运行应用程序的root电话,在这种情况下,您必须根据应用程序的软件包名称手动构建路径。

You should have this permission in your manifest: 您应该在清单中拥有此权限:

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

After that you can use PackageManager to clear cache of all program: 之后,您可以使用PackageManager清除所有程序的缓存:

PackageManager  pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
    if (m.getName().equals("freeStorage")) {
        // Found the method I want to use
        try {
            long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
            m.invoke(pm, desiredFreeStorage , null);
        } catch (Exception e) {
            // Method invocation failed. Could be a permission problem
        }
        break;
    }
}

This requests that Android clear enough cache files so that there is 8GB free. 这要求Android清除足够的缓存文件,以便有8GB空闲。 If you set this number high enough you should achieve what you want (that Android will delete all of the files in the cache). 如果你设置这个数字足够高,你应该达到你想要的(Android将删除缓存中的所有文件)。 When you call freeStorage() it checks to see if the amount of storage (in this case 8GB) is available for cache files. 当您调用freeStorage()时,它会检查存储量(在这种情况下为8GB)是否可用于缓存文件。 If not, it starts to delete files from application's cache directories by deleting the oldest files first. 如果没有,它会首先删除最旧的文件,从应用程序的缓存目录中删除文件。 It continues to delete files until either there are not longer any files to delete, or it has freed up the amount of storage you requested (in this case 8GB). 它会继续删除文件,直到没有任何文件要删除,或者它释放了您请求的存储量(在这种情况下为8GB)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM