Android Support Annotations注解库之优化代码

导言:
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");


若是项目有需求,可以大量的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值