是否可以检查另一个应用程序是否已被授予 Android 6.0+ 的权限?

Is it possible to check if another app has been granted a permission on Android 6.0+?

我想检查另一个应用程序是否已被授予 "dangerous" 或 "system" 级别的权限。

我试过加载另一个应用程序的上下文并调用 packageContext.checkCallingPermission(permission)。但是,文档说 returns

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

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

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

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> 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 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;
        }
    }

有关更多信息,请查看: