Log输出某APP拥有的权限
PackageManager pm = context.getPackageManager();
PackageInfo pi;
try {
// 参数2必须是PackageManager.GET_PERMISSIONS
pi = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
String[] permissions = pi.requestedPermissions;
if(permissions != null){
for(String str : permissions){
Log.i(TAG, str);
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
上述代码中,packageName是目标APP的包名。
以List集合返回某APP拥有的权限信息
基于上述代码,修改为如下方式:
List<PermissionInfo> permissionInfoList = new ArrayList<PermissionInfo>();
PackageManager pm = context.getPackageManager();
PackageInfo pi;
try {
pi = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
String[] permissions = pi.requestedPermissions;
if(permissions != null){
for(String str : permissions){
PermissionInfo permissionInfo = context.getPackageManager().getPermissionInfo(str, 0);
permissionInfoList.add(permissionInfo);
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return permissionInfoList;
基于上述代码,便可获得更为丰富的权限信息。
具体信息可参考该页面内的getAppPermissionList
和showAppPermissionList
方法。