是否可以检查是否已向其他应用授予Android 6.0+的权限? [英] Is it possible to check if another app has been granted a permission on Android 6.0+?

查看:231
本文介绍了是否可以检查是否已向其他应用授予Android 6.0+的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查另一个应用程序是否已被授予危险"或系统"级别的权限.

I want to check if another app has been granted a "dangerous" or "system" level permission.

我尝试加载另一个应用程序的上下文并调用

I have tried loading another app's context and calling packageContext.checkCallingPermission(permission). However, the documentation says it returns

PERMISSION_GRANTED(如果允许调用pid/uid的权限),否则为PERMISSION_DENIED(如果不允许).

PERMISSION_GRANTED if the calling pid/uid is allowed that permission, or PERMISSION_DENIED if it is not.

是否可以检查是否已向另一个应用程序授予权限?

Is it possible to check if another app has been granted a permission?

这是我的尝试(我在意识到它会检查调用的pid/uid之前似乎写过它,而且似乎没有考虑上下文):

Here is my attempt (I wrote it before realizing it checks the calling pid/uid and doesn't seem to consider the context):

void checkAllGrantedPermissions(Context context) {
  PackageManager pm = context.getPackageManager();

  // get all installed apps with info about what permissions they requested.
  List<PackageInfo> packageInfos = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);

  // Get the hidden method PermissionInfo#protectionToString(int) so we can log info about the requested permission
  Method protectionToString;
  try {
    protectionToString = PermissionInfo.class.getDeclaredMethod("protectionToString", int.class);
    if (!protectionToString.isAccessible()) protectionToString.setAccessible(true);
  } catch (NoSuchMethodException e) {
    throw new RuntimeException(e);
  }

  // loop through all installed apps
  for (PackageInfo packageInfo : packageInfos) {

    if (packageInfo.requestedPermissions == null) {
      // No permissions are requested in the AndroidManifest
      continue;
    }

    String appName = packageInfo.applicationInfo.loadLabel(pm).toString();
    String packageName = packageInfo.packageName;

    // loop through all requested permissions in the AndroidManifest
    for (String permission : packageInfo.requestedPermissions) {

      PermissionInfo permissionInfo;
      try {
        permissionInfo = pm.getPermissionInfo(permission, 0);
      } catch (PackageManager.NameNotFoundException e) {
        Log.i(TAG, String.format("unknown permission '%s' found in '%s'", permission, packageName));
        continue;
      }

      // convert the protectionLevel to a string (not necessary, but useful info)
      String protLevel;
      try {
        protLevel = (String) protectionToString.invoke(null, permissionInfo.protectionLevel);
      } catch (Exception ignored) {
        protLevel = "????";
      }

      // Create the package's context to check if the package has the requested permission
      Context packageContext;
      try {
        packageContext = context.createPackageContext(packageName, 0);
      } catch (PackageManager.NameNotFoundException wtf) {
        continue;
      }

      int ret = packageContext.checkCallingPermission(permission);
      if (ret == PackageManager.PERMISSION_DENIED) {
        Log.i(TAG, String.format("%s [%s] is denied permission %s (%s)",
            appName, packageName, permission, protLevel));
      } else {
        Log.i(TAG, String.format("%s [%s] has granted permission %s (%s)",
            appName, packageName, permission, protLevel));
      }
    }
  }
}

推荐答案

是否可以检查是否已向另一个应用程序授予权限?

Is it possible to check if another app has been granted a permission?

是的.您可以在以下位置为每个 <uses-permission> 标记检索标志使用 PackageInfo.requestedPermissionsFlags 来显示给定软件包这些标志使用 PackageInfo.REQUESTED_PERMISSION_GRANTED 进行按位运算.

Yep. You can retrieve the flags for each <uses-permission> tag in the Manifest for a given package using PackageInfo.requestedPermissionsFlags then compare those flags using a bitwise operation with PackageInfo.REQUESTED_PERMISSION_GRANTED.

我想检查另一个应用是否已被授予危险"或 系统"级别的权限.

I want to check if another app has been granted a "dangerous" or "system" level permission.

您可以使用 PackageManager.getPermissionInfo 然后比较 PermissionInfo.PROTECTION_DANGEROUS 之一或 PermissionInfo.PROTECTION_SIGNATURE .

You can perform this check using PackageManager.getPermissionInfo then compare PermissionInfo.protectionLevel to one of PermissionInfo.PROTECTION_DANGEROUS or PermissionInfo.PROTECTION_SIGNATURE.

例如:

    final PackageManager pm = getPackageManager();
    // Loop each package requesting <manifest> permissions
    for (final PackageInfo pi : pm.getInstalledPackages(GET_PERMISSIONS)) {
        final String[] requestedPermissions = pi.requestedPermissions;
        if (requestedPermissions == null) {
            // No permissions defined in <manifest>
            continue;
        }
        // Loop each <uses-permission> tag to retrieve the permission flag
        for (int i = 0, len = requestedPermissions.length; i < len; i++) {
            final String requestedPerm = requestedPermissions[i];
            // Retrieve the protection level for each requested permission
            int protLevel;
            try {
                protLevel = pm.getPermissionInfo(requestedPerm, 0).protectionLevel;
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Unknown permission: " + requestedPerm, e);
                continue;
            }
            final boolean system = protLevel == PROTECTION_SIGNATURE;
            final boolean dangerous = protLevel == PROTECTION_DANGEROUS;
            final boolean granted = (pi.requestedPermissionsFlags[i]
                    & REQUESTED_PERMISSION_GRANTED) != 0;
        }
    }

有关更多信息,请查看:

  • The Settings application
  • R.attr.protectionLevel
  • PermissionInfo

这篇关于是否可以检查是否已向其他应用授予Android 6.0+的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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