简体   繁体   English

是否可以检查是否已向其他应用授予Android 6.0+的权限?

[英]Is it possible to check if another app has been granted a permission on 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) . 我尝试加载另一个应用程序的上下文并调用packageContext.checkCallingPermission(permission) However, the documentation says it returns 但是,文档说它返回

PERMISSION_GRANTED if the calling pid/uid is allowed that permission, or PERMISSION_DENIED if it is not. 如果允许调用pid / uid的权限为PERMISSION_GRANTED,否则为PERMISSION_DENIED。

Is it possible to check if another app has been granted a permission? 是否可以检查是否已授予另一个应用程序许可?

Here is my attempt (I wrote it before realizing it checks the calling pid/uid and doesn't seem to consider the context): 这是我的尝试(我在意识到它检查调用的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? 是否可以检查是否已授予另一个应用程序许可?

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 . 您可以检索标记每个<uses-permission>标签中的清单使用给定的包PackageInfo.requestedPermissionsFlags然后比较使用了位运算的标志PackageInfo.REQUESTED_PERMISSION_GRANTED

I want to check if another app has been granted a "dangerous" or "system" level permission. 我想检查另一个应用程序是否已被授予“危险”或“系统”级别的权限。

You can perform this check using PackageManager.getPermissionInfo then compare PermissionInfo.protectionLevel to one of PermissionInfo.PROTECTION_DANGEROUS or PermissionInfo.PROTECTION_SIGNATURE . 您可以使用PackageManager.getPermissionInfo进行此检查,然后将PermissionInfo.protectionLevelPermissionInfo.PROTECTION_DANGEROUSPermissionInfo.PROTECTION_SIGNATURE之一进行比较。

For example : 例如

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

For more information check out: 有关更多信息,请查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android 6.0+ 中获得将文件写入 SDCard 的权限 - How to get permission to write files into SDCard in Android 6.0+ Android:在授予请勿打扰权限时检测实例 - Android: Detect the instance when DO NOT DISTURB permission has been granted 使用root访问权限在Android 6.0+中修改另一个应用的SQLite数据库 - Modify another app's SQLite database in Android 6.0+ using root access 如何检查位置已由用户在android 6.0中为我的应用程序提供? - how to check location permission has been given by the user in android 6.0 for my application? 动态链接(Android 应用链接)不提示“打开方式”Android 6.0+ - Dynamic Links (Android App Links) not to prompt "open with" Android 6.0+ 如何检查我的应用程序是否已被授予 root 访问权限? - How to check if my app has been granted root access? 如何检查用户是否在 React Native Android 中授予了相机权限? - How to check if user has granted camera permission in React Native Android? 无法在Android 6.0+上关闭AlertDialog - AlertDialog not dismiss on Android 6.0+ 未授予Android应用中的互联网权限 - Internet permission in Android app not granted Android 6.0 = PERMISSION_NOT_GRANTED(具有CONTACTS和LOCATION权限的问题) - Android 6.0 = PERMISSION_NOT_GRANTED (Issue with CONTACTS and LOCATION permission)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM