1

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

1 回答 1

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"/>
于 2013-10-25T12:24:24.713 回答