# PermissionChecker

# PermissionChecker

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/content/PermissionChecker.java+class:androidx.core.content.PermissionChecker)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/content/PermissionChecker "View this page in Java")

<br />

```
class PermissionChecker
```

<br />

*** ** * ** ***

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](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#M())and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than[M](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#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                                    |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PERMISSION_DENIED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED())` = -1` Permission result: The permission is denied.                                                 |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PERMISSION_DENIED_APP_OP](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED_APP_OP())` = -2` Permission result: The permission is denied because the app op is not allowed. |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [PERMISSION_GRANTED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_GRANTED())` = 0` Permission result: The permission is granted.                                               |

|                                   ### Public functions                                    |
|-------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [checkCallingOrSelfPermission](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#checkCallingOrSelfPermission(android.content.Context,java.lang.String))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`, permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` 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.                                                                                                                                                                                                                                                                                          |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [checkCallingPermission](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#checkCallingPermission(android.content.Context,java.lang.String,java.lang.String))`(` ` context: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`,` ` packageName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?` `)` Checks whether the IPC you are handling has a given permission and whether the app op that corresponds to this permission is allowed.                                                                                                                                                                                 |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [checkPermission](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#checkPermission(android.content.Context,java.lang.String,int,int,java.lang.String))`(` ` context: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`,` ` pid: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` uid: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` packageName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?` `)` 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. |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [checkSelfPermission](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#checkSelfPermission(android.content.Context,java.lang.String))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`, permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` Checks whether your app has a given permission and whether the app op that corresponds to this permission is allowed.                                                                                                                                                                                                                                                                                                                                        |

## Constants

### PERMISSION_DENIED

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val PERMISSION_DENIED = -1: Int
```

Permission result: The permission is denied.  

### PERMISSION_DENIED_APP_OP

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val PERMISSION_DENIED_APP_OP = -2: Int
```

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

### PERMISSION_GRANTED

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val PERMISSION_GRANTED = 0: Int
```

Permission result: The permission is granted.  

## Public functions

### checkCallingOrSelfPermission

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun checkCallingOrSelfPermission(context: Context, permission: String): Int
```

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](https://developer.android.com/reference/android/content/Context.html)      | Context for accessing resources. |
| `permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | The permission to check.         |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The permission check result which is either[PERMISSION_GRANTED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_GRANTED())or[PERMISSION_DENIED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED())or[PERMISSION_DENIED_APP_OP](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED_APP_OP()). |

### checkCallingPermission

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun checkCallingPermission(
    context: Context,
    permission: String,
    packageName: String?
): Int
```

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](https://developer.android.com/reference/android/content/Context.html)          | Context for accessing resources.                                                                 |
| `permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)     | The permission to check.                                                                         |
| `packageName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?` | The package name making the IPC. If null the the first package for the calling UID will be used. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The permission check result which is either[PERMISSION_GRANTED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_GRANTED())or[PERMISSION_DENIED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED())or[PERMISSION_DENIED_APP_OP](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED_APP_OP()). |

### checkPermission

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun checkPermission(
    context: Context,
    permission: String,
    pid: Int,
    uid: Int,
    packageName: String?
): Int
```

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](https://developer.android.com/reference/android/content/Context.html)          | Context for accessing resources.                                                                     |
| `permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)     | The permission to check.                                                                             |
| `pid: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                  | The process id for which to check.                                                                   |
| `uid: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                  | The uid for which to check.                                                                          |
| `packageName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?` | The package name for which to check. If null the the first package for the calling UID will be used. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The permission check result which is either[PERMISSION_GRANTED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_GRANTED())or[PERMISSION_DENIED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED())or[PERMISSION_DENIED_APP_OP](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED_APP_OP()). |

### checkSelfPermission

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun checkSelfPermission(context: Context, permission: String): Int
```

Checks whether your app has a given permission and whether the app op that corresponds to this permission is allowed.  

|                                           Parameters                                            |
|-------------------------------------------------------------------------------------------------|----------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)      | Context for accessing resources. |
| `permission: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | The permission to check.         |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The permission check result which is either[PERMISSION_GRANTED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_GRANTED())or[PERMISSION_DENIED](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED())or[PERMISSION_DENIED_APP_OP](https://developer.android.com/reference/kotlin/androidx/core/content/PermissionChecker#PERMISSION_DENIED_APP_OP()). |