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

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

问题描述

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

似乎大多数方法在 Android M 中都不再适用了.

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

PackageManager pm = getPackageManager();//获取 PackageManager 上的所有方法Method[] 方法 = pm.getClass().getDeclaredMethods();for(方法m:方法){如果 (m.getName().equals("freeStorage")) {//找到我想使用的方法尝试 {长期所需的FreeStorage = 8 * 1024 * 1024 * 1024;//请求 8GB 的​​可用空间m.invoke(pm, requiredFreeStorage , null);} 捕获(异常 e){//方法调用失败.可能是权限问题}休息;}}

结合此权限

此代码在低于 Android 6.0 的设备上运行良好.但是在使用 Android 6.0 的设备上会导致此异常

java.lang.IllegalArgumentException:参数数量错误;预期 3,得到 2

GitHub 上有一个开源的清理器,由于这个原因停止了开发:

<块引用><块引用>

部分不兼容 Android 6.0 及更新版本

Android 6.0 开始,CLEAR_APP_CACHE 权限好像没有了不再适用于常规应用程序,并且由于此权限是需要清理内部缓存,缓存清理器不能清理 Android 6.0 及更新版本的内部缓存.然而,清洁仍然支持外部缓存.

真的没有办法删除Android M设备上的App Cache吗?

Google Setting App 如何处理?不可能是新方法:

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

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

更新就像例外答案所解释的那样,在 Android M 及更高版本的设备上,似乎无法在没有 root 的情况下删除应用程序缓存.但如果我找到方法,我会在这里发布......

解决方案

Android M 中是否有删除所有应用或某些应用的缓存的选项?

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

<块引用>

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

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

<块引用>

设置"应用如何处理?

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

if (v == mClearCacheButton) {//延迟初始化观察者如果(mClearCacheObserver == null){mClearCacheObserver = new ClearCacheObserver();}mPm.deleteApplicationCacheFiles(mPackageName, mClearCacheObserver);}

因此,设置"应用正在使用隐藏方法 PackageManager#deleteApplicationCacheFiles(String, IPackageDataObserver).它可以这样做是因为它拥有系统级权限 "android.permission.CLEAR_APP_USER_DATA" (第三个权限-party app 不能保存).

<小时>

外部缓存

<块引用>

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

这在 Android 6.0+ 上仍然是可能的.我没有查看您提到的应用程序的源代码,但我认为您需要做的就是请求 WRITE_EXTERNAL_STORAGE 权限,使用 PackageManager 获取所有已安装的包,获取应用的外部缓存目录,并删除目录.

<小时>

Root 权限

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

PackageManager pm = getPackageManager();列表<ApplicationInfo>已安装的应用程序 = pm.getInstalledApplications(0);for (ApplicationInfo applicationInfo : installedApplications) {尝试 {上下文 packageContext = createPackageContext(applicationInfo.packageName, 0);列表<文件>目录 = 新的 ArrayList();directory.add(packageContext.getCacheDir());如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {Collections.addAll(directories, packageContext.getExternalCacheDirs());} 别的 {directory.add(packageContext.getExternalCacheDir());}StringBuilder command = new StringBuilder("rm -rf");对于(文件目录:目录){command.append(" "" + directory.getAbsolutePath() + """);}Shell.SU.run(command.toString());} catch (PackageManager.NameNotFoundException wtf) {}}

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

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

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

Combined with this permission

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

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 

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

Partial incompatibility with Android 6.0 and newer

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.

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

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)

because I think it does not delete the Cache of certain apps, but of enough apps to clear the expected ram size... But the Settings app can clear the Cache of certain apps

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

解决方案

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

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.

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

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 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).


External Cache

However, cleaning of external cache is still supported.

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 Access

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天全站免登陆