如何清除Android应用缓存?

Sra*_*rao 6 android

我正在编写一个应用程序,可以编程方式清除设备上安装的所有第三方应用程序的应用程序缓存.以下是Android 2.2的代码段

public static void trimCache(Context myAppctx) {

    Context context = myAppctx.createPackageContext("com.thirdparty.game", 
            Context.CONTEXT_INCLUDE_CO|Context.CONTEXT_IGNORE_SECURITY);

    File cachDir = context.getCacheDir();        
    Log.v("Trim", "dir " + cachDir.getPath());

    if (cachDir!= null && cachDir.isDirectory()) {

        Log.v("Trim", "can read " + cachDir.canRead());
        String[] fileNames = cachDir.list();
        //Iterate for the fileName and delete
    }
}
Run Code Online (Sandbox Code Playgroud)

我的清单有以下权限:

android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES

现在的问题是打印了缓存目录的名称,但文件列表cachDir.list()始终返回null.我无法删除缓存目录,因为文件列表始终为null.

还有其他方法可以清除应用程序缓存吗?

Com*_*are 6

"android.permission.CLEAR_APP_CACHE"android.permission.DELETE_CACHE_FILES"

普通的SDK应用程序无法保留DELETE_CACHE_FILES权限.虽然您可以保留CLEAR_APP_CACHE,但Android SDK中没有任何内容可以让您清除应用的缓存.

还有其他方法可以清除应用程序缓存吗?

您可以通过删除该缓存中的文件来清除自己的缓存.