Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

android - How to get all granted permissions of a app

I want to get all granted permissions. I know I can get all requested permissions by packageinfo.requestedPermissions but I want to know list of granted permissions and granted permissions can be lesser then requested in case of android M. So I just wanted to know that is there way that I can get list of all granted permissions?

I know from list of requested permission I can check for that permission weather granted or not but I want to know list of all granted permission. Don't want to check for every requested permission.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A simple function that returns all the permissions that have been requested and granted for a given package could look like this:

List<String> getGrantedPermissions(final String appPackage) {
    List<String> granted = new ArrayList<String>();
    try {
        PackageInfo pi = getPackageManager().getPackageInfo(appPackage, PackageManager.GET_PERMISSIONS);
        for (int i = 0; i < pi.requestedPermissions.length; i++) {
            if ((pi.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) {
                granted.add(pi.requestedPermissions[i]);
            }
        }
    } catch (Exception e) {
    }
    return granted;
}

Note that this requires API level 16 or above, but that should hopefully not be an issue these days.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...