Skip to content

Most visited

Recently visited

navigation

PermissionChecker

public final class PermissionChecker
extends Object

java.lang.Object
   ↳ android.support.v4.content.PermissionChecker


This class provides permission check APIs that verify both the permission and the associated app op for this permission if such is defined.

In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than 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.

Summary

Constants

int PERMISSION_DENIED

Permission result: The permission is denied.

int PERMISSION_DENIED_APP_OP

Permission result: The permission is denied because the app op is not allowed.

int PERMISSION_GRANTED

Permission result: The permission is granted.

Public methods

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.

Inherited methods

From class java.lang.Object

Constants

PERMISSION_DENIED

added in version 23.0.0
int PERMISSION_DENIED

Permission result: The permission is denied.

Constant Value: -1 (0xffffffff)

PERMISSION_DENIED_APP_OP

added in version 23.0.0
int PERMISSION_DENIED_APP_OP

Permission result: The permission is denied because the app op is not allowed.

Constant Value: -2 (0xfffffffe)

PERMISSION_GRANTED

added in version 23.0.0
int PERMISSION_GRANTED

Permission result: The permission is granted.

Constant Value: 0 (0x00000000)

Public methods

checkCallingOrSelfPermission

added in version 23.0.0
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.

Parameters
context Context: Context for accessing resources.

permission String: The permission to check.

Returns
int The permission check result which is either PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.

checkCallingPermission

added in version 23.0.0
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.

Parameters
context Context: Context for accessing resources.

permission String: The permission to check.

packageName String: The package name making the IPC. If null the the first package for the calling UID will be used.

Returns
int The permission check result which is either PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.

checkPermission

added in version 23.0.0
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.

Parameters
context Context: Context for accessing resources.

permission String: The permission to check.

pid int: The process id for which to check.

uid int: The uid for which to check.

packageName String: The package name for which to check. If null the the first package for the calling UID will be used.

Returns
int The permission check result which is either PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.

checkSelfPermission

added in version 23.0.0
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.

Parameters
context Context: Context for accessing resources.

permission String: The permission to check.

Returns
int The permission check result which is either PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)