【问题标题】:How can I clear the Android app cache?如何清除 Android 应用缓存?
【发布时间】:2012-12-13 07:02:20
【问题描述】:

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

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

我的清单具有以下权限:

android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES

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

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

【问题讨论】:

  • 你不需要root设备吗?

标签: android


【解决方案1】:

“android.permission.CLEAR_APP_CACHE”android.permission.DELETE_CACHE_FILES”

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

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

欢迎您通过删除缓存中的文件来清除您自己的缓存。

【讨论】:

  • 在获得正确权限的情况下,您可以删除外部存储中其他应用程序的缓存文件夹(仅限)。不一样,但很多应用程序使用这些文件夹作为放置临时文件的主要位置。
  • 我想知道用户什么时候可以点击清除缓存我可以获得通知或其他如何获得??
  • @KOUSIKdaniel:我认为您的进程将在缓存被清除之前终止,但我不确定。我不知道您会收到任何其他通知。
  • @CommonsWare 谢谢你,如果你得到信息,请分享。
  • @CommonsWare 确切的问题是,当用户按下并从最近的应用程序按钮清除缓存时,我想要收到通知
【解决方案2】:

查看 android.content.pm.PackageManager.clearApplicationUserData:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/content/pm/PackageManager.java/ 该类中的其他隐藏方法也可能有用。

如果您以前从未使用过隐藏方法,您可以使用 Java 反射访问隐藏方法。

【讨论】:

    【解决方案3】:

    poate iti 合并 asta

    static int clearCacheFolder(final File dir, final int numDays) {
    
            int deletedFiles = 0;
            if (dir!= null && dir.isDirectory()) {
                try {
                    for (File child:dir.listFiles()) {
    
                        //first delete subdirectories recursively
                        if (child.isDirectory()) {
                            deletedFiles += clearCacheFolder(child, numDays);
                        }
    
                        //then delete the files and subdirectories in this dir
                        //only empty directories can be deleted, so subdirs have been done first
                        if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                            if (child.delete()) {
                                deletedFiles++;
                            }
                        }
                    }
                }
                catch(Exception e) {
                    Log.e("ATTENTION!", String.format("Failed to clean the cache, error %s", e.getMessage()));
                }
            }
            return deletedFiles;
        }
    
        public static void clearCache(final Context context, final int numDays) {
            Log.i("ADVL", String.format("Starting cache prune, deleting files older than %d days", numDays));
            int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
            Log.i("ADVL", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
        }
    

    【讨论】:

    • 成功了。我更喜欢:long now = new Date().getTime(); 在循环之前,而不是为每个文件声明一个新的now 实例。
    【解决方案4】:

    我不确定这在约定方面有多合适,但到目前为止,这在我的 Global Application 课程中对我有效:

        File[] files = cacheDir.listFiles();
        for (File file : files){
            file.delete();
        }
    

    当然,这并不涉及嵌套目录,这可以通过像这样的递归函数来完成(未对子目录进行广泛测试):

    deleteFiles(cacheDir);
    
    private void deleteFiles(File dir){
        if (dir != null){
            if (dir.listFiles() != null && dir.listFiles().length > 0){
                // RECURSIVELY DELETE FILES IN DIRECTORY
                for (File file : dir.listFiles()){
                    deleteFiles(file);
                }
            } else {
                // JUST DELETE FILE
                dir.delete();
            }
        }
    }
    

    我没有使用File.isDirectory,因为它在我的测试中不可靠。

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 2011-02-20
      • 2013-01-08
      相关资源
      最近更新 更多