Most visited

Recently visited

RequiresPermission

public abstract @interface RequiresPermission
implements Annotation

android.support.annotation.RequiresPermission


表示注释元素需要(或可能需要)一个或多个权限。

要求单个权限的示例:


   @RequiresPermission(Manifest.permission.SET_WALLPAPER)
   public abstract void setWallpaper(Bitmap bitmap) throws IOException;

   @RequiresPermission(ACCESS_COARSE_LOCATION)
   public abstract Location getLastKnownLocation(String provider);
 
Example of requiring at least one permission from a set:

   @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
   public abstract Location getLastKnownLocation(String provider);
 
Example of requiring multiple permissions:

   @RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
   public abstract Location getLastKnownLocation(String provider);
 
Example of requiring separate read and write permissions for a content provider:

   @RequiresPermission.Read(@RequiresPermission(READ_HISTORY_BOOKMARKS))
   @RequiresPermission.Write(@RequiresPermission(WRITE_HISTORY_BOOKMARKS))
   public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
 

在参数上指定时,注释表示该方法需要一个取决于参数值的权限。 例如,考虑android.app.Activity.startActivity(android.content.Intent)

public void startActivity(@RequiresPermission Intent intent) { ... }
 
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:
Intent intent = new Intent(Intent.ACTION_CALL);
   startActivity(intent);
 
and the actual permission requirement for this particular intent is described on the Intent name itself:

   @RequiresPermission(Manifest.permission.CALL_PHONE)
   public static final String ACTION_CALL = "android.intent.action.CALL";
 

Summary

Nested classes

@interface RequiresPermission.Read

指定读取操作需要给定权限。

@interface RequiresPermission.Write

指定写操作需要给定权限。

Inherited methods

From interface java.lang.annotation.Annotation

Hooray!