【问题标题】:Clear cache in android application清除android应用程序中的缓存
【发布时间】:2020-07-25 06:00:41
【问题描述】:

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

【问题讨论】:

    标签: java android caching


    【解决方案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"/>
    

    【讨论】:

    • 我可以在onListItemClick 方法中编写这段代码吗?我可以吗?
    • 如果你想有一个对话。在调用 packageManager 的方法之前,您需要调用对话框。所以调用对话框一个 onClick ok 按钮调用 freeStorage 方法
    • 哦,好的,所以“freestorage”是方法的名称?
    • 好的,完全理解..我怎么才能看到清除了多少mb?
    • desiredFreeStorage 是免费缓存吗?因为如果用 toast 调用它会返回 0
    猜你喜欢
    • 2015-09-07
    • 2013-01-08
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多