且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除Android M中所有应用程序的应用程序缓存?

更新时间:2023-11-20 21:55:46

是否可以删除Android M中所有应用程序或某些应用程序的缓存?

Is there an option to delete the cache of all apps or certain apps in Android M?

第三方应用程序无法删除Android 6.0+中另一个应用程序的缓存. Manifest.permission.CLEAR_APP_CACHE 的保护级别已从危险"更改为Android 6.0+中的签名|特权"或系统|签名".现在,只有使用固件密钥签名的应用程序才能拥有此权限.

A third-party app cannot delete the cache of another app in Android 6.0+. The protection level of Manifest.permission.CLEAR_APP_CACHE changed from "dangerous" to "signature|privileged" or "system|signature" in Android 6.0+. Now, only apps signed with the firmware's key can hold this permission.

真的没有办法在装有Android M的设备上删除应用程序缓存吗?

Is there really no way to delete app cache on devices with Android M?

除非该应用程序已安装为系统应用程序或您具有root访问权限,否则无法删除Android 6.0+上的应用程序缓存.

Unless the app is installed as a system app or you have root access, there is no way to delete app cache on Android 6.0+.

设置"应用如何处理?

How does the Settings app handle it?

当然,Android是开源的.让我们看一下代码.在 AppStorageSettings.java 第172-178行,我们发现:

Android is, of course, open source. Lets look at the code. In AppStorageSettings.java lines 172 - 178 we find:

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

因此,设置"应用使用的是隐藏方法(第三方应用程序不能拥有的权限).

So, the Settings app is using the hidden method PackageManager#deleteApplicationCacheFiles(String, IPackageDataObserver). It can do this because it holds the system level permission "android.permission.CLEAR_APP_USER_DATA" (a permission a third-party app cannot hold).

外部缓存

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

However, cleaning of external cache is still supported.

在Android 6.0或更高版本上仍然可以这样做.我没有查看您提到的应用程序的源代码,但我假设您需要做的就是请求WRITE_EXTERNAL_STORAGE权限,使用PackageManager获取所有已安装的软件包,获取应用程序的

This is still possible on Android 6.0+. I haven't looked at the source code for the app you mentioned but I would assume all you need to do is request the WRITE_EXTERNAL_STORAGE permission, get all installed packages using PackageManager, get the app's external cache directory, and delete the directory.

根访问权限

当然,如果您具有root用户访问权限,则可以删除另一个应用程序的缓存.这是使用根访问权限删除 all 应用程序缓存的快速示例.您可以使用Chainfire的 libsuperuser 在根shell中运行命令:

Of course, if you have root access you can delete another app's cache. Here is a quick example of using root access to delete all app cache. You can use Chainfire's libsuperuser to run commands in a root shell:

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