compile 'com.github.hotchemi:permissionsdispatcher:4.6.0'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:4.6.0'
@RuntimePermissions注解:用于标注在需要申请权限的Activity或Fragment
@RuntimePermissions
class RememberToRemindActivity:SimpleActivity(){
}
@NeedsPermission注解:用于标注需要获取的权限,多个权限用逗号分隔,,权限申请成功后会调用被注解的方法使用如下
@NeedsPermission(Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.READ_CALENDAR,Manifest.permission.WRITE_CALENDAR)
fun permissionsSuccess(){
//这是权限申请成功后会调用的方法
}
这两个注解后,需要build project,build完在onCreat中调用新生成的方法
permissionsSuccessWithPermissionCheck()//PermissionsDispatcher生成的辅助类
@OnShowRationale注解:这个方法用于告诉用户你接下来申请的权限是干嘛的,说服用户给你权限
@OnShowRationale(Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.READ_CALENDAR,Manifest.permission.WRITE_CALENDAR)
fun berforpermissions(request:PermissionRequest){
//此方法需要传入PermissionRequest对象,此对象有两个方法proceed()让权限请求继续,cancel()让请求中断。
}
@OnPermissionDenied注解:用于标注如果权限请求失败,但是用户没有勾选不再询问的时候执行的方法
@OnPermissionDenied(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR
)
fun permissionsDenied() {
permissionsSuccessWithPermissionCheck()//此处调用上面的辅助类,重新申请权限
}
实现这个方法还需要实现回调方法
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
onRequestPermissionsResult(requestCode, grantResults)
}
@OnNeverAskAgain注解:用于标注如果权限请求失败,而且用户勾选不再询问的时候执行的方法
@OnNeverAskAgain(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR
)
fun permissionsNeverAskAgain() {
//自己需要的方法
}
以上注解,除了第一个和第二个是必须要的注解,其他的都是非必要的注解。