最近要整理个老项目,权限那块需要改,就回顾了下权限相关的,在此整理下。
这里用到的是 permissionsdispatcher
下面是简单的使用,详细的去看
1、先检查下 build.gradle 中是否有
apply plugin: "kotlin-kapt"
这句话,没有的话再顶部加上;
2、添加依赖
implementation "org.permissionsdispatcher:permissionsdispatcher:4.5.0"
kapt "org.permissionsdispatcher:permissionsdispatcher-processor:4.5.0"
3、开始使用
第一种用法:
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.OnPermissionDenied
import permissions.dispatcher.RuntimePermissions
@RuntimePermissions
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
permissionNeedWithPermissionCheck()
}
@NeedsPermission(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
)
fun permissionNeed() {
Log.e("permissionNeed", "权限获取成功")
}
@OnPermissionDenied(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
)
fun permissionDenied() {
Log.e("permissionDenied"