2012-11-23 32 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