2012-11-23 31 views
1

我试图在下面给出的代码中列出所选应用程序中每个权限的保护级别。但我不知道如何完成它。如何获得每个权限的保护级别?

ArrayList<String> list_permission = new ArrayList<String>(); 
     String[] reqp = info.requestedPermissions; 
     if (reqp != null) { 
      for (i = 0; i < reqp.length; i++) { 

       k = i + 1; 

       String a = reqp[i]; 
       if (a.contains("android.permission.")) { 
        String aa[] = a.split("android.permission."); 
        list_permission.add(aa[1]); 
       } else { 
        list_permission.add(a); 
       } 

      } 

     } 

谁能帮我这个...只是要添加的权限前面的保护级别。

+0

是否要检查已安装软件包的权限级别?如果是,请检查此API http://developer.android.com/reference/android/content/pm/PermissionInfo.html#protectionLevel –

+0

http://rupertrawnsley.blogspot.de/2011/11/android-permissions-protection- levels.html甚至这给了整个权限保护水平......但不知道如何编写它的应用程序即时做 – Romi

回答

3

您可以使用PackageManagergetPermissionInfo()方法获得PermissionInfo对象的任何特定权限。 PermissionInfo对象具有属性Protection Lavel,可用于检查任何权限的保护级别...您可以根据PermissoinInfo类中定义的常量(如PROTECTION_FLAG_SYSTEM)对其进行检查。

像下面的代码:

for (PermissionInfo permission : packageInfo.permissions) { 
    // Dump permission info 
    String protectionLevel; 
    switch(permission.protectionLevel) { 
    case PermissionInfo.PROTECTION_NORMAL : protectionLevel = "normal"; break; 
    case PermissionInfo.PROTECTION_DANGEROUS : protectionLevel = "dangerous"; break; 
    case PermissionInfo.PROTECTION_SIGNATURE : protectionLevel = "signature"; break; 
    case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protectionLevel = "signatureOrSystem"; break; 
    default : protectionLevel = "<unknown>"; break; 
    } 
    Log.i("PermissionCheck", permission.name + " " + protectionLevel); 
    } 

EDIT1:

  String a = reqp[i]; 
      if (a.contains("android.permission.")) { 



    try { 
     PermissionInfo pi = getPackageManager().getPermissionInfo(a, PackageManager.GET_META_DATA); 
     String protctionLevel = "unknown"; 

     switch(pi.protectionLevel) { 
     case PermissionInfo.PROTECTION_NORMAL : protctionLevel = "normal"; break; 
     case PermissionInfo.PROTECTION_DANGEROUS : protctionLevel = "dangerous"; break; 
     case PermissionInfo.PROTECTION_SIGNATURE : protctionLevel = "signature"; break; 
     case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protctionLevel = "signatureOrSystem"; break; 
     case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break; 
     default : protctionLevel = "<unknown>"; break; 
     } 
       list_permission.add(a+"  "+protctionLevel); 
    } catch (NameNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

      } else { 
       list_permission.add(a); 
      } 

以下行只会在API级别16或以上的工作:

 case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break; 
+0

因此它给出了作为0,1,2,3的中间值,所以0是正常的,1是危险的,2是签名,3是系统? – Romi

+0

不使用int值使用比较返回值与PermissionInfo类中定义的常量来检查保护级别类似于您已共享的链接... –

+0

如果您提供的代码我很容易理解兄弟.. – Romi

-1

//获取的权限core android包

PackageInfo packageInfo = getPackageManager().getPackageInfo("android", PackageManager.GET_PERMISSIONS); 
if (packageInfo.permissions != null) { 
    // For each defined permission 
    for (PermissionInfo permission : packageInfo.permissions) { 
    // Dump permission info 
    String protectionLevel; 
    switch(permission.protectionLevel) { 
    case PermissionInfo.PROTECTION_NORMAL : protectionLevel = "normal"; break; 
    case PermissionInfo.PROTECTION_DANGEROUS : protectionLevel = "dangerous"; break; 
    case PermissionInfo.PROTECTION_SIGNATURE : protectionLevel = "signature"; break; 
    case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protectionLevel = "signatureOrSystem"; break; 
    default : protectionLevel = "<unknown>"; break; 
    } 
    Log.i("PermissionCheck", permission.name + " " + protectionLevel); 
    } 
} 
+0

我如何得到它所选的权限reqp [i]。 – Romi