RequiresPermission
@Target([AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER]) class RequiresPermission
androidx.annotation.RequiresPermission |
Denotes that the annotated element requires (or may require) one or more permissions.
Example of requiring a single permission:
<code>
@RequiresPermission(Manifest.permission.SET_WALLPAPER)
public abstract void setWallpaper(Bitmap bitmap) throws IOException;
@RequiresPermission(ACCESS_COARSE_LOCATION)
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring at least one permission from a set:
<code>
@RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring multiple permissions:
<code>
@RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring separate read and write permissions for a content provider:
<code>
@RequiresPermission.Read(@RequiresPermission(READ_HISTORY_BOOKMARKS))
@RequiresPermission.Write(@RequiresPermission(WRITE_HISTORY_BOOKMARKS))
public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
</code>
When specified on a parameter, the annotation indicates that the method requires a permission which depends on the value of the parameter. For example, consider android.app.Activity.startActivity(android.content.Intent)
:
<code>public void startActivity(@RequiresPermission Intent intent) { ... }
</code>
Notice how there are no actual permission names listed in the annotation. The actual permissions required will depend on the particular intent passed in. For example, the code may look like this:
<code>Intent intent = new Intent(Intent.ACTION_CALL);
startActivity(intent);
</code>
and the actual permission requirement for this particular intent is described on the Intent name itself:
<code>
@RequiresPermission(Manifest.permission.CALL_PHONE)
public static final String ACTION_CALL = "android.intent.action.CALL";
</code>
Summary
Nested classes |
|
---|---|
Specifies that the given permission is required for read operations. |
|
Specifies that the given permission is required for write operations. |
Public constructors |
|
---|---|
Denotes that the annotated element requires (or may require) one or more permissions. |
Properties |
|
---|---|
Array<String> |
Specifies a list of permission names that are all required. |
Array<String> |
Specifies a list of permission names where at least one is required |
Boolean |
If true, the permission may not be required in all cases (e.g. it may only be enforced on certain platforms, or for certain call parameters, etc. |
String |
The name of the permission that is required, if precisely one permission is required. |
Public constructors
<init>
RequiresPermission(value: String, allOf: Array<String>, anyOf: Array<String>, conditional: Boolean)
Denotes that the annotated element requires (or may require) one or more permissions.
Example of requiring a single permission:
<code>
@RequiresPermission(Manifest.permission.SET_WALLPAPER)
public abstract void setWallpaper(Bitmap bitmap) throws IOException;
@RequiresPermission(ACCESS_COARSE_LOCATION)
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring at least one permission from a set:
<code>
@RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring multiple permissions:
<code>
@RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);
</code>
Example of requiring separate read and write permissions for a content provider:
<code>
@RequiresPermission.Read(@RequiresPermission(READ_HISTORY_BOOKMARKS))
@RequiresPermission.Write(@RequiresPermission(WRITE_HISTORY_BOOKMARKS))
public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
</code>
When specified on a parameter, the annotation indicates that the method requires a permission which depends on the value of the parameter. For example, consider android.app.Activity.startActivity(android.content.Intent)
:
<code>public void startActivity(@RequiresPermission Intent intent) { ... }
</code>
Notice how there are no actual permission names listed in the annotation. The actual permissions required will depend on the particular intent passed in. For example, the code may look like this:
<code>Intent intent = new Intent(Intent.ACTION_CALL);
startActivity(intent);
</code>
and the actual permission requirement for this particular intent is described on the Intent name itself:
<code>
@RequiresPermission(Manifest.permission.CALL_PHONE)
public static final String ACTION_CALL = "android.intent.action.CALL";
</code>
Properties
conditional
val conditional: Boolean
If true, the permission may not be required in all cases (e.g. it may only be enforced on certain platforms, or for certain call parameters, etc.