如何删除Android M中所有应用程序的应用程序缓存? [英] How to delete app cache for all apps in Android M?

查看:155
本文介绍了如何删除Android M中所有应用程序的应用程序缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

似乎大多数方式在Android M中都不再起作用.

Seems like most ways don't work anymore in Android M.

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

As a reference I used this Code out of this discussion : 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;
    }
} 

结合此权限

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

此代码在低于Android 6.0的设备上正常运行.但是在装有Android 6.0的设备上,会导致此异常

This Code works fine on devices lower than Android 6.0. But on devices with Android 6.0 it results in this exception

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

GitHub上有一个开放源代码的清洁程序,它由于以下原因而停止了开发:

There is an open source cleaner on GitHub, which stopped development with this reason:

与Android 6.0及更高版本部分不兼容

Partial incompatibility with Android 6.0 and newer

从Android 6.0开始,似乎没有CLEAR_APP_CACHE权限 不再适用于常规应用程序,因为此权限是 清理内部缓存所需,缓存清理器无法 在Android 6.0及更高版本上清除内部缓存.但是,清洁 仍然支持外部缓存.

Starting with Android 6.0, CLEAR_APP_CACHE permission seems to be no longer available to regular applications and since this permission is required for cleaning of internal cache, Cache Cleaner is not able to clean internal cache on Android 6.0 and newer. However, cleaning of external cache is still supported.

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

Is there really no way, to delete App Cache on devices with Android M ?

Google设置应用如何处理它? 不能是新方法:

How does the Google Setting App handle it ? It can't be the new method:

public abstract void freeStorage(String volumeUuid, long freeStorageSize,
        IntentSender pi)

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

更新 就像例外答案所解释的那样,在没有安装Android M及更高版本的设备上,如果没有root用户,似乎无法删除App Cache.但是如果有办法,我会在这里发布....

UPDATE Like the excepted answer explains, there seems to be no way to delete tha App Cache without root on Devices with Android M and above. But I will post it here if I find a way....

推荐答案

是否可以删除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) {
  }
}

这篇关于如何删除Android M中所有应用程序的应用程序缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆