导言:
Android Support Annotations是从19.1版本引入的注解库,可优化代码,增加可读性,又可减少代码的报错
使用:
1:导包
implementation 'com.android.support:support-annotations:23.4.0'
2:Support Annotations分类
2.1:Nullness注解
@Nullable注解可以用来标识特定的参数或者返回值可以为null
@NonNull注解可以用来标识参数不能为null
2.2:Resource Type 注解
2.3:Threading 注解
Threading注解:
@UiThread UI线程
@MainThread 主线程
@WorkerThread 子线程
@BinderThread 绑定线程
2.4:Value Constraints注解:@Size, @IntRange, @FloatRange
2.5:Permissions 注解: @RequiresPermission
@RequiresPermission(Manifest.permission.SET_WALLPAPER)
public abstract void setWallpaper(Bitmap bitmap) throws IOException;
//如果你至少需要权限集合中的一个,你可以使用anyOf属性
@RequiresPermission(anyOf = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);
//如果你同时需要多个权限,你可以用allOf属性
@RequiresPermission(allOf = {
Manifest.permission.READ_HISTORY_BOOKMARKS,
Manifest.permission.WRITE_HISTORY_BOOKMARKS})
public static final void updateVisitedHistory(ContentResolver cr, String url, boolean real) ;
//对于intents的权限,可以直接在定义的intent常量字符串字段上标注权限需求(他们通常都已经被@SdkConstant
//注解标注过了)
@RequiresPermission(android.Manifest.permission.BLUETOOTH)
public static final String ACTION_REQUEST_DISCOVERABLE =
"android.bluetooth.adapter.action.REQUEST_DISCOVERABLE";
//对于content providers的权限,你可能需要单独的标注读和写的权限访问,所以可以用@Read或者@Write标注
//每一个权限需求
@RequiresPermission.Read(@RequiresPermission(READ_HISTORY_BOOKMARKS))
@RequiresPermission.Write(@RequiresPermission(WRITE_HISTORY_BOOKMARKS))
public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
若是项目有需求,可以大量的使用