Kosh
Kosh

Reputation: 6334

Get permission details programmatically

Is there anyway in android sdk where they provide an information for specific permission? Like how google play does, whenever you click on a permission you are able to read what does this permission do. I have this function to return me specific permissions for an application

public static List<String> getAppPermissions(Context context, String packageName) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        return Arrays.asList(info.requestedPermissions);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return new ArrayList<>();
    }
} 

By this code im getting only permission names, but I'm wondering if there is any such api that return permissions and their details.

Upvotes: 7

Views: 4283

Answers (1)

Kosh
Kosh

Reputation: 6334

to answer my question, PermissionInfo is actually the right class to get a description about any android permission for example:

@Nullable
public static PermissionInfo getPermissionInfo(@NonNull Context context, @NonNull String permission) {
    try {
        return context.getPackageManager().getPermissionInfo(permission, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

above method will return PermissionInfo instance if it could parse the permission name given to it, and then simply you could call loadDescription to load the permission description.

Upvotes: 2

Related Questions