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

查看:23
本文介绍了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- 这只发生在androidM"版本(从开发者站点闪现预览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)

我知道这是一个黑客行为,而谷歌并没有为此带来一些官方 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.

非常感谢您的帮助

推荐答案

一个在 Android 5 上提出的错误,关于任何应用程序如何使用常规权限清除所有缓存文件,但除非具有签名级权限,否则无法清除一个包的缓存文件.细节在哪里

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 的 目的不是清除所有缓存文件,但要释放 X 量的空间,例如通过 Play 商店在它尝试下载和安装应用程序之前.所以是有原因的使用它可以很好地与系统配合使用,但没有理由让应用程序使用只是盲目擦除所有缓存文件的方法应用程序(仅用于设置"应用程序 UI).

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

如果确实不是应用程序错误,即您没有弄乱权限并且它适用于 Marshmallow/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 的代码出来时,我们会更清楚(当前可用的是 5.1.1 - 链接到 PackageManager 的 freeStorageAndNotify).

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