Android:清除所有应用程序的缓存? [英] Android: Clear Cache of All Apps?

查看:40
本文介绍了Android:清除所有应用程序的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我制作的 Android 应用程序中,我希望能够以编程方式清除设备上所有应用程序的缓存.这已经被问过很多次了:以编程方式清除应用缓存?反映清除Android应用缓存的方法清除其他应用程序缓存每个人都说没有root是不可能的.

In an Android app I am making, I want to be able to programmatically clear the cache of all of the apps on the device. This has been asked many times before: Clearing app cache programmatically? Reflecting methods to clear Android app cache Clear another applications cache and everyone says it's not possible without root.

然而,情况显然并非如此.如果您查看 Google Play 中的 App Cache Cleaner、History Eraser、1Tap Cleaner、Easy History Cleaner 和无数其他类似应用程序(所有这些都不需要 root),您就会意识到它们都可以做到这一点.因此,这是可能的,但我找不到任何公开的例子来说明如何做到这一点.

However, this is clearly not the case. If you look at the apps App Cache Cleaner, History Eraser, 1Tap Cleaner, Easy History Cleaner, and countless other similar apps in the Google Play (all of which don't require root) you will realize they can all do this. Therefore, it IS possible to do, but I just cannot find any public examples how to do this.

有人知道所有这些应用在做什么吗?

Does anyone know what all of those apps are doing?

谢谢

推荐答案

这里有一种不需要 IPPackageDataObserver.aidl 的方法:

Here's a way to do it that doesn't require IPackageDataObserver.aidl:

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;
    }
}

您需要在清单中包含此内容:

You will need to have this in your manifest:

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

这要求 Android 清除足够的缓存文件,以便有 8GB 可用空间.如果您将此数字设置得足够高,您应该可以实现您想要的(Android 将删除缓存中的所有文件).

This requests that Android clear enough cache files so that there is 8GB free. 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 保留所有应用程序缓存目录中所有文件的 LRU(最近最少使用)列表.当您调用 freeStorage() 时,它会检查是否有存储量(在本例中为 8GB)可用于缓存文件.如果没有,它会通过首先删除最旧的 文件来开始从应用程序的缓存目录中删除文件.它会继续删除文件,直到不再有任何文件要删除,或者释放了您请求的存储空间(在本例中为 8GB).

The way this works is that Android keeps an LRU (Least Recently Used) list of all the files in all application's cache directories. When you call freeStorage() it checks to see if the amount of storage (in this case 8GB) is available for cache files. 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).

这篇关于Android:清除所有应用程序的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆