Android M反射方法freeStorageAndNotify异常 [英] Android M reflection method freeStorageAndNotify exception

查看:531
本文介绍了Android M反射方法freeStorageAndNotify异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反射方法freeStorageAndNotify:

I'm using reflection method freeStorageAndNotify:

Method freeStorageAndNotify = null;
freeStorageAndNotify = service.packageManager.getClass().getMethod(
                "freeStorageAndNotify", long.class, IPackageDataObserver.class);
freeStorageAndNotify.invoke(PackageManager.class, maxCache + freeSpace, packageDataObserver);

这会导致InvocationTargetException:

This causes InvocationTargetException:

java.lang.SecurityException: Neither user 10199 nor current process has android.permission.CLEAR_APP_CACHE.

一些要点: -我已经有android.permission.CLEAR_APP_CACHE -仅在Android"M"版本中才会发生这种情况(从开发者网站刷新了预览SDK)

Some points: - I already have android.permission.CLEAR_APP_CACHE - This happens only in android "M" Version (Flashed the preview sdk from developer site)

我知道这是一种黑客行为,而Google并未为此提供一些官方API, 但是有这么多清洁应用程序可以一键清除所有设备缓存,因此,如果有人知道如何通过另一种解决方法来绕过此问题,我将很乐意看到.

I know this is a hack, and google doesn't bring some official API for that, But there are so many cleaning apps which cleans all the device cache in one click, so if someone know how to bypass this issue with another workaround i'll be happy to see that.

非常感谢您的帮助

推荐答案

There was a bug raised on Android 5 regarding how any app can wipe out all cache files with a regular permission, but cannot wipe out one package's cache files except with a signature-level permission. It's details where

PackageManager具有一个deleteApplicationCacheFiles(),用于从一个程序包中删除缓存. 此方法在SDK中是隐藏的,因此需要 DELETE_CACHE_FILES,签名级权限.

PackageManager has a deleteApplicationCacheFiles() to delete the cache from one package. This method is hidden from the SDK, and it requires DELETE_CACHE_FILES, a signature-level permission.

PackageManager还有一个freeStorageAndNotify()方法,可以删除 缓存所有程序包中的文件.此方法在SDK中是隐藏的,并且 它需要CLEAR_APP_CACHE权限,该权限仅被标记 为危险".

PackageManager also has a freeStorageAndNotify() method, to delete cache files from all packages. This method is hidden from the SDK, and it requires the CLEAR_APP_CACHE permission, which is merely flagged as "dangerous".

有人建议让DELETE_CACHE_FILES的级别放松, CLEAR_APP_CACHE应该提高其级别.

It was proposed to either that DELETE_CACHE_FILES should have its level relaxed, CLEAR_APP_CACHE should have its level raised.

框架工程师做出了回应

请注意,freeStorageAndNotify's用途并非要清除所有缓存 文件,但可以释放X的空间,例如通过Play商店 尝试下载并安装应用之前.所以有原因 使用它可以很好地与系统配合使用,但没有理由让应用 使用只是盲目擦除所有缓存文件的方法 应用程序(就在设置"应用程序用户界面中).

Note that freeStorageAndNotify's purpose is not to wipe out all cache files, but to free up X amount of space, for example by play store before it tries to download and install an app. So there are reasons to use it that work well with the system, but no reason for an app to use the method that just blindly erases all cache files for a single app (that is just there for the Settings app UI).

如果确实不是应用程序错误,即您没有弄乱权限,并且它可以在棉花糖/6/api 23上运行,而不能在其他应用程序上运行,这只能意味着它已成为签名级权限同样,如DELETE_CACHE_FILES.

If indeed it is not an app error i.e. you haven't messed up the permissions and it works on Marshmallow / 6 / api 23 and not others that could only mean it became a signature level permission as well, like DELETE_CACHE_FILES.

签名|系统许可,意味着它只能由 用固件的签名密钥签名或已安装的应用程序 在系统分区上(例如,由具有root权限的设备用户).如前所述 在此答案中.

A signature|system permission, meaning that it can only be held by apps that are signed with the firmware's signing key or are installed on the system partition (e.g., by a rooted device user). As described in this answer.

考虑到它们的预期用途/愿景(没有理由让应用程序使用一种方法只是盲目擦除单个应用程序的所有缓存文件),这将是有道理的.该错误甚至可能限制了它.当Android 6的代码发布时,我们会更好地了解(当前可用的是

This would make sense, considering their intended use / their vision (no reason for an app to use the method that just blindly erases all cache files for a single app). It may have even been restricted as a result of that bug. When Android 6's code will come out we will know better (current available is 5.1.1 - link to PackageManager's freeStorageAndNotify).

这篇关于Android M反射方法freeStorageAndNotify异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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