android - 权限保护级别错误?

标签 android android-permissions android-package-managers android-6.0-marshmallow

我正在尝试针对 Android Marshmallow 调整我的代码。

我编写了一个函数来检查权限是否可撤销(PROTECTION_NORMALPROTECTION_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

如果您使用问题中显示的代码,则不会。最好的情况是,您的代码指示 protectionLevelnormal 还是 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/

相关文章:

java - 在 Android 上使用 Facebook 登录

java - 在 MapFragment 中调用 onRequestPermissionResult 时出现 Android Studio @androidx.annotation.NonNull 错误

android - 当应用程序处于 "pending publication"阶段并被评论者/测试者(Google Play 商店)使用/评论时,InstallerPackageName 是什么?

android 线性布局背景选择器

android - 在Android中播放媒体

java - 如何在扩展 Activity 的类中更改 ListView 的不同列表项的不同文本颜色?

java - List<UsageStats> 返回空

android - 如何授予权限 Manifest.permission.MANAGE_DOCUMENTS android 6.0

Android显示没有 list 权限的拨号盘

android - 从当前应用程序加载另一个应用程序