简体   繁体   English

如何获得应用程序的所有授予的权限

[英]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? 我知道我可以通过packageinfo.requestedPermissions获得所有请求的权限,但是我想知道授予权限的列表,如果使用android M,授予的权限可以更少,所以我只想知道那是我可以获取列表的方式所有授予的权限?

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. 不想检查每个请求的权限。

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. 请注意,这需要API级别16或更高级别,但希望这些天不会成为问题。

Have you tried this? 你有尝试过吗? for this you should iterate through your array 为此,您应该遍历数组

boolean permissionGranted = PermissionChecker.checkSelfPermission(MyActivity.this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;

there is also method for acquiring needed Parmissions like below: 还有一种获取所需配额的方法,如下所示:

private static String[] PERMISSIONS = {
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
};

ActivityCompat.requestPermissions(
                        MyActivity.this,
                        PERMISSIONS,
                        REQUEST_EXTERNAL_STORAGE
                );

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                 //handle
            }
        }
    }
}

just pack all your permissions to PERMISSIONS array, but be aware that on 6.0+ this will cause some system dialog appear 只需将所有权限打包到PERMISSIONS数组中即可,但是请注意,在6.0+上,这将导致出现一些系统对话框

You can check permission one by one and add to list: 您可以一一检查权限并添加到列表中:

// Should we show an explanation? //我们应该显示一个解释吗?

        List<String> listPermissionsNeeded = new ArrayList<>();
        // No explanation needed, we can request the permission.
        if((ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED))
        {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if((ContextCompat.checkSelfPermission(context,
                Manifest.permission.GET_ACCOUNTS)
                != PackageManager.PERMISSION_GRANTED))
        {
            listPermissionsNeeded.add(Manifest.permission.GET_ACCOUNTS);
        }
        if((ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED))
        {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        if((ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED))
        {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if((ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED))
        {
            listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(context,
                    listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                    MY_PERMISSIONS_REQUEST_MULTIPLE_PERMISSION);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM