public final class PermissionChecker extends Object
In the new permission model permissions with protection level
dangerous are runtime permissions. For apps targeting Build.VERSION_CODES.M
and above the user may not grant such permissions or revoke
them at any time. For apps targeting API lower than Build.VERSION_CODES.M
these permissions are always granted as such apps do not expect
permission revocations and would crash. Therefore, when the
user disables a permission for a legacy app in the UI the
platform disables the APIs guarded by this permission making
them a no-op which is doing nothing or returning an empty
result or default error.
It is important that when you perform an operation on behalf of another app you use these APIs to check for permissions as the app may be a legacy app that does not participate in the new permission model for which the user had disabled the "permission" which is achieved by disallowing the corresponding app op.
Modifier and Type | Class and Description |
---|---|
static interface |
PermissionChecker.PermissionResult |
Modifier and Type | Field and Description |
---|---|
static int |
PERMISSION_DENIED
Permission result: The permission is denied.
|
static int |
PERMISSION_DENIED_APP_OP
Permission result: The permission is denied because the app op is not allowed.
|
static int |
PERMISSION_GRANTED
Permission result: The permission is granted.
|
Modifier and Type | Method and Description |
---|---|
static int |
checkCallingOrSelfPermission(Context context,
String permission)
Checks whether the IPC you are handling or your app has a given permission
and whether the app op that corresponds to this permission is allowed.
|
static int |
checkCallingPermission(Context context,
String permission,
String packageName)
Checks whether the IPC you are handling has a given permission and whether
the app op that corresponds to this permission is allowed.
|
static int |
checkPermission(Context context,
String permission,
int pid,
int uid,
String packageName)
Checks whether a given package in a UID and PID has a given permission
and whether the app op that corresponds to this permission is allowed.
|
static int |
checkSelfPermission(Context context,
String permission)
Checks whether your app has a given permission and whether the app op
that corresponds to this permission is allowed.
|
public static final int PERMISSION_GRANTED
public static final int PERMISSION_DENIED
public static final int PERMISSION_DENIED_APP_OP
public static int checkPermission(@NonNull Context context, @NonNull String permission, int pid, int uid, String packageName)
context
- Context for accessing resources.permission
- The permission to check.pid
- The process id for which to check.uid
- The uid for which to check.packageName
- The package name for which to check. If null the
the first package for the calling UID will be used.PERMISSION_GRANTED
or PERMISSION_DENIED
or PERMISSION_DENIED_APP_OP
.public static int checkSelfPermission(@NonNull Context context, @NonNull String permission)
context
- Context for accessing resources.permission
- The permission to check.PERMISSION_GRANTED
or PERMISSION_DENIED
or PERMISSION_DENIED_APP_OP
.public static int checkCallingPermission(@NonNull Context context, @NonNull String permission, String packageName)
context
- Context for accessing resources.permission
- The permission to check.packageName
- The package name making the IPC. If null the
the first package for the calling UID will be used.PERMISSION_GRANTED
or PERMISSION_DENIED
or PERMISSION_DENIED_APP_OP
.public static int checkCallingOrSelfPermission(@NonNull Context context, @NonNull String permission)
context
- Context for accessing resources.permission
- The permission to check.PERMISSION_GRANTED
or PERMISSION_DENIED
or PERMISSION_DENIED_APP_OP
.