在Android应用程序中清除缓存

时间:2013-10-25 12:21:50

标签: java android caching

我不认为这是重复的。好吧,我解释了我需要的东西..我有一个安装在我的设备中的所有应用程序的列表..通过点击我需要显示一个对话框,上面写着“你想要清除缓存吗?”用“是”或当然是“否”。我找到了这个教程: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;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

这可以帮助你:

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"/>