当前位置: 首页 > 知识库问答 >
问题:

如何删除所有应用程序缓存在AndroidM?

高鸿振
2023-03-14

似乎大多数方法在Android M中都不起作用了。

作为参考,我在讨论中使用了以下代码:Android:清除所有应用程序的缓存?

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"/>
java.lang.IllegalArgumentException: Wrong number of arguments; expected 3, got 2 
public abstract void freeStorage(String volumeUuid, long freeStorageSize,
        IntentSender pi)

因为我认为它不会删除某些应用程序的缓存,而是删除足够多的应用程序,以清除预期的ram大小...但是设置应用程序可以清除某些应用程序的缓存

更新就像例外答案解释的那样,在Android M及以上的设备上,似乎没有办法删除没有root的tha应用缓存。但如果我想办法,我会把它贴在这里....

共有1个答案

杜禄
2023-03-14

AndroidM中是否有删除所有应用程序或某些应用程序缓存的选项?

第三方应用程序在Android 6.0+中不能删除另一个应用程序的缓存。在Android 6.0+中,manifest.permission.clear_app_cache的保护级别从“危险”改为“signatureprivileged”或“systemsignature”。现在,只有用固件密钥签名的应用程序才能持有此权限。

在AndroidM的设备上真的没有办法删除应用缓存吗?

if (v == mClearCacheButton) {
    // Lazy initialization of observer
    if (mClearCacheObserver == null) {
        mClearCacheObserver = new ClearCacheObserver();
    }
    mPm.deleteApplicationCacheFiles(mPackageName, mClearCacheObserver);
}

因此,设置应用程序使用隐藏方法PackageManager#DeleteApplicationCacheFiles(String,IPackageDataObserver)。它可以做到这一点,因为它拥有系统级权限“Android.permission.clear_app_user_data”(第三方应用程序不能拥有的权限)。

外部缓存

但是,仍然支持清除外部缓存。

PackageManager pm = getPackageManager();
List<ApplicationInfo> installedApplications = pm.getInstalledApplications(0);
for (ApplicationInfo applicationInfo : installedApplications) {
  try {
    Context packageContext = createPackageContext(applicationInfo.packageName, 0);
    List<File> directories = new ArrayList<>();
    directories.add(packageContext.getCacheDir());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      Collections.addAll(directories, packageContext.getExternalCacheDirs());
    } else {
      directories.add(packageContext.getExternalCacheDir());
    }

    StringBuilder command = new StringBuilder("rm -rf");
    for (File directory : directories) {
      command.append(" \"" + directory.getAbsolutePath() + "\"");
    }

    Shell.SU.run(command.toString());
  } catch (PackageManager.NameNotFoundException wtf) {
  }
}
 类似资料:
  • 我试图开发一个android应用程序,可以擦除其他应用程序的缓存数据,我试图浏览所有的博客,但没有一个对我有效,我可以通过以下代码清除我的应用程序的缓存 我想清除其他应用程序的缓存,可以任何机构请帮助我,如果我错了请纠正我,提前谢谢。

  • 您好,我已经在codova开发了一个带有ionic framework的移动应用程序。我已经构建了这个应用程序,并开始在android mobile上进行测试。注销时,单击“我已在后面添加此代码”: 但在手机测试时间,当我注销时 我的控制器代码: 我的注销代码: 我访问数据的身份是:

  • 问题内容: 我正在使用二级缓存和查询缓存。我可以知道如何以编程方式清除所有缓存吗? 问题答案: 清除会话缓存使用 要清除二级缓存,请使用此代码段

  • del 清空指定缓存 await this.app.redis.del('name') flushall 清空所有缓存 await this.app.redis.flushall() lpop 从数组最左边删除一项 await this.app.redis.lpop('userList') rpop 从数组最右边删除一项 await this.app.redis.rpop('userList')

  • void clear_all_cache(int expire time) This clears the entire template cache. As an optional parameter, you can supply a minimum age in seconds the cache files must be before they will get cleared. 清除所