2011-12-26 139 views

回答

1

從字面上看,我相信這個問題是關於如何簡單地獲取可能的權限,而不是在一個包運行時的權限列表。假設這是真的,這只是一個反思問題。

你會做這樣的事情(不編譯):

List<Field> permissionFields = new ArrayList<Field>(); 
Field[] fields = Manifest.permission.class.getDeclaredFields(); 
for (Field declField : fields) { 
    if ((declField.getType() == String.class) && Modifier.isFinal(declField.getModifiers())) { 
    permissionFields.add(declField); 
    } 
} 

的「permissionFields」列表認爲是允許值的對象列表。您可以使用「Field.getObject(obj)」方法來獲取字符串值(好奇的是,似乎並沒有出現「Field.getString(obj)」方法,儘管還有其他幾個「Field.get(obj) )「方法(可能是因爲String不是原語))。

+0

正是我需要的。謝謝! – Phil 2011-12-27 02:34:04

0

下面是代碼:

Log.d(TAG, "Getting permissions for " + packageName); 
try { 
    PackageInfo pkgInfo = getPackageManager().getPackageInfo(
       packageName, 
       PackageManager.GET_PERMISSIONS 
      ); 
    PermissionInfo[] permissions = pkgInfo.permissions; 
    if (permissions == null) { 
    permissionsField.setText("No declared permissions"); 
    } 
    else { 
    String permissionsText = ""; 
    for (int i = 0; i < permissions.length; i++) { 
     switch (permissions[i].protectionLevel) { 
      case 0: 
       permissionsText += permissions[i].name + ":::" + "NORMAL" + "\n"; 
       break; 
      case 1: 
       permissionsText += permissions[i].name + ":::" + "DANGEROUS" + "\n"; 
       break; 
      case 2: 
       permissionsText += permissions[i].name + ":::" + "SIGNATURE" + "\n"; 
       break; 
      case 3: 
       permissionsText += permissions[i].name + ":::" + "SIGNATUREORSYSTEM" + "\n"; 
       break; 
     } 
    } 
    permissionsField.setText(permissionsText); 
    } 

    String[] requestedPermissions = pkgInfo.requestedPermissions; 
    if (requestedPermissions == null) { 
    requestedPermissionsField.setText("No requested permissions"); 
    } 
    else { 
    String reqPermText = ""; 
    for (int i = 0; i < requestedPermissions.length; i++) { 
     reqPermText += requestedPermissions[i] + "\n"; 
    } 
    requestedPermissionsField.setText(reqPermText); 
    } 
} 
catch (PackageManager.NameNotFoundException e) { 
    permissionsField.setText("Package Not Found"); 
    requestedPermissionsField.setText("Package Not Found"); 
} 
+0

請更好地閱讀問題。如果是我所要求的,這將是一個很好的迴應。 – Phil 2011-12-27 02:35:37