清除棉花糖中所有应用程序的缓存?

我尝试使用以下代码来解决: [参考: Android:清除所有应用程序的缓存吗?

I try to solve using below code: [Reference: Android: Clear Cache of All Apps?

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

并在AndroidMenifest.xml文件中添加权限

and add permission in AndroidMenifest.xml file

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

但是当我运行这段代码时,然后抛出异常:

But when i run this code, then throw exception :

java.lang.IllegalArgumentException: Wrong number of arguments; expected 3, got 2

关于克服此异常的任何建议或以编程方式清除所有应用程序的另一种解决方案.谢谢

Any suggestion to overcome this exception or another solution for clear cache of all apps programmatically. Thanks



 1 条回答