PackageInfo: requestedPermissions vs permissions

2019-06-07 09:44发布

问题:

What is the difference between requestedPermissions and permissions?

PackageInfo _pi = getPackageManager().getPackageInfo(this.getPackageName(), 
                                               PackageManager.GET_PERMISSIONS);

// permissions
PermissionInfo[] _permissions = _pi.permissions;

// requestedPermissions
String[] _requestedPermissions = _pi.requestedPermissions;

Is it about application's permissions and OS's permissions?

Thanks

回答1:

As in the documentation

public PermissionInfo[]

Array of all <permission> tags included under <manifest>, or null if there were none.

and

int[] requestedPermissions

Array of all <uses-permission> tags included under <manifest>, or null if there were none.

so permissionInfo will have the attributes in manifest tag in manifest for e.g.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.androidtest"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">

and

requstedPermissions will return the permissions in <uses-permission>tag for e.g

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />


回答2:

if you want get all permissions pre checked in manifest you must use code below:

private bool PermissionInManifest (string permission)
{
    var permissions = this.Activity.PackageManager.GetPackageInfo(this.Activity.PackageName, PackageInfoFlags.Permissions);
    var requestedPermissions = permissions.RequestedPermissions.ToList();
    return requestedPermissions.Contains(permission);
}

if you want check permission granted by user or not, you can use:

private bool PermissionGranted (string permission)
{
    return this.Activity.PackageManager.CheckPermission (permission, this.Activity.PackageName) == Permission.Granted;
}