我正在尝试针对 Android Marshmallow 调整我的代码。
我编写了一个函数来检查权限是否可撤销(PROTECTION_NORMAL
和 PROTECTION_SIGNATURE
在安装时授予)。
在 API-22 上运行时,Manifest.permission.READ_PHONE_STATE
返回 protectionLevel=PermissionInfo.PROTECTION_DANGEROUS
,即 is what I expected .
但在 API-22 上,Manifest.permission.INSTALL_SHORTCUT
还会返回 protectionLevel=PermissionInfo.PROTECTION_DANGEROUS
,即 is wrong from the documentation .
这是怎么发生的? 我的代码有什么问题:
final PermissionInfo permissionInfo = packageManager.getPermissionInfo(permission, 0);
switch (permissionInfo.protectionLevel) {
case PermissionInfo.PROTECTION_NORMAL:
case PermissionInfo.PROTECTION_SIGNATURE:
return false;
default:
return true;
}
最佳答案
Manifest.permission.INSTALL_SHORTCUT also returns protectionLevel=PermissionInfo.PROTECTION_DANGEROUS
如果您使用问题中显示的代码,则不会。最好的情况是,您的代码指示 protectionLevel
是 normal
还是 signature
并且没有设置其他位。
How does that happen?
protectionLevel
是一个位掩码。您没有正确比较位掩码。
int coreBits=info.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
coreBits
将是核心 PROTECTION_
值之一。 protectionLevel
可能不会,因为可能设置了更高阶的位(例如,PROTECTION_FLAG_PRE23
)。而且,在 Android 6.0 上,coreBits
报告 INSTALL_SHORTCUT
是正常
权限。
参见this sample project有关使用 PermissionInfo.PROTECTION_MASK_BASE
的演示。
关于android - 权限保护级别错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32201297/