gpt4 book ai didi

Android M反射方法freeStorageAndNotify异常

转载 作者:IT老高 更新时间:2023-10-28 23:34:03 24 4
gpt4 key购买 nike

我使用的是反射方法freeStorageAndNotify:

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

这会导致 InvocationTargetException:

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

几点:- 我已经有 android.permission.CLEAR_APP_CACHE- 这只发生在 android "M"版本中(从开发者网站刷了预览 sdk)

我知道这是一个 hack,谷歌并没有为此提供一些官方 API,但是有很多清理应用程序可以一键清理所有设备缓存,所以如果有人知道如何用另一种解决方法绕过这个问题,我会很高兴看到这一点。

非常感谢您的帮助

最佳答案

a bug在 Android 5 上提出了关于任何应用程序如何使用常规权限清除所有缓存文件,但不能清除一个包的缓存文件,除非具有签名级权限。这是细节在哪里

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 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 的级别。

一位框架工程师回应

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

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 - link to PackageManager's freeStorageAndNotify)。

关于Android M反射方法freeStorageAndNotify异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31989637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com