java - 清除android应用程序中的缓存

标签 java android caching

我不认为这是重复的。好吧,我解释一下我需要什么。我有一个安装在我的设备中的所有应用程序的列表。通过单击我需要显示一个对话框,上面写着“你想要清除缓存吗?”用"is"或者当然是“否”。我找到了这个教程:http://android-sample-code.blogspot.it/2012/01/how-to-clear-cache-data-in-android.html但似乎删除了数据文件夹。我想知道的是;有区别吗?是否有仅用于清除缓存而不是应用程序数据的代码?

代码:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);    
        /**Clear cache*/
        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;
            }
        }
    }

最佳答案

希望这对您有帮助:

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

不要忘记权限:

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

关于java - 清除android应用程序中的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19589752/

相关文章:

java - Ehcache Java Spring MVC 和分页 @Cacheable

java - string.matches(regex) 返回 false,虽然我认为它应该是 true

java - 使用 htmlunit 单击 javascript anchor 似乎不起作用

android - 使用 Sencha 的应用程序中的文本字段样式在 Android 设备上崩溃

c# - 如何使用 C# 查找系统缓存和空闲内存

当内存在 ubuntu 中仍然可用时发生 Java 内存不足

java - 如何在 Windows 中运行子 java 进程?

java - 使用java读取war文件中的Manifest.mf

android - android api < 16 中的 Codenameone LocalNotification 以及当应用程序从最近删除时

android maps circle overlay,动态改变半径?