最近要整理个老项目,权限那块需要改,就回顾了下权限相关的,在此整理下。
这里用到的是
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", "权限获取失败")
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Log.e(
"requestCode = $requestCode",
" per = ${permissions[0]} ;grantResults = ${grantResults}"
)
onRequestPermissionsResult(requestCode, grantResults)
}
}
说明:
1、为了验证多个权限的处理。我同时申请了 读写、拍照;刚写完上面的方法,onRequestPermissionsResult 可能会报错(报红),就clean、rebuild
2、permissionNeed 是自己定义的方法,用于处理
申请的权限都被同意的
情况。注意,(以这里为例)读写、拍照都通过了,就会走到这个方法里面
3、permissionDenied 是自己定义的方法,如果 申请的权限中有任何一个拒绝了,就会走到这里。
clean、rebuild后,就不报错了,这个时候,在需要触发权限申请的地方,调用(我这里是 Activity – onCreate)
permissionNeedWithPermissionCheck()
就会开始申请权限。
这个方法是build的时候,自动生成的。名字分两部分 permissionNeed+WithPermissionCheck。前半部分,是刚自己写的
下面是日志
1、读写、拍照都授权
requestCode = 0: per = android.permission.READ_EXTERNAL_STORAGE ;grantResults = [I@62ae615
permissionNeed: 权限获取成功
2、读写同意、拍照拒绝---读写拒绝、拍照同意---读写、拍照都拒绝
requestCode = 0: per = android.permission.READ_EXTERNAL_STORAGE ;grantResults = [I@62ae615
permissionDenied: 权限获取失败
第二种用法
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
),
1
);
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Log.e(
"requestCode = $requestCode",
" per = ${permissions[0]} ;grantResults = ${grantResults}"
)
if (requestCode == 1) {
when (permissions[0]) {
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE -> {
Log.e("回调", "读写")
if (PermissionUtils.verifyPermissions(*grantResults)) {
Log.e("回调", "读写授权成功")
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.CAMERA
),
1
);
} else {
Log.e("回调", "读写授权失败")
}
}
Manifest.permission.CAMERA -> {
Log.e("回调", "相册")
if (PermissionUtils.verifyPermissions(*grantResults)) {
Log.e("回调", "相册授权成功")
} else {
Log.e("回调", "相册授权失败")
}
}
}
}
}
}
说明:
1、这里我用了链式的。也就是 先读写后拍照 的顺序。requestCode 我都用了1,具体到项目中根据实际情况来
2、如果刚开始的读写被拒绝了,根据上面的操作,就不会再去申请拍照了。具体需要具体区分
以上两种权限的申请、回调,各有优缺点。根据自己的项目实际情况选择
至于判断有无某个(某些)权限,常用方法有2个:
PermissionUtils.hasSelfPermissions
PermissionUtils.verifyPermissions
对应的源码是
/**
* Returns true if the Activity or Fragment has access to all given permissions.
*
* @param context context
* @param permissions permission list
* @return returns true if the Activity or Fragment has access to all given permissions.
*/
public static boolean hasSelfPermissions(Context context, String... permissions) {
for (String permission : permissions) {
if (permissionExists(permission) && !hasSelfPermission(context, permission)) {
return false;
}
}
return true;
}
/**
* Checks all given permissions have been granted.
*
* @param grantResults results
* @return returns true if all permissions have been granted.
*/
public static boolean verifyPermissions(int... grantResults) {
if (grantResults.length == 0) {
return false;
}
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
上面的第二种方法中,因为 onRequestPermissionsResult 直接返回了 grantResults,就用了 PermissionUtils.verifyPermissions。
PermissionUtils.hasSelfPermissions 的用法是:
PermissionUtils.hasSelfPermissions(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
)
PermissionUtils.hasSelfPermissions(
this,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)