Android 动态权限
June 12, 2019, 5:33 p.m.
read: 1173
1. 添加权限到 AndroidManifest.xml
2. 添加以下语句到build.gradle=>dependencies
implementation 'com.mylhyl:acp:1.2.0'
然后sync project
3.添加Java语句以动态请求权限
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.MODIFY_AUDIO_SETTINGS,// 此处写清单文件里面定义的那些权限)
/*以下为自定义提示语、按钮文字
.setDeniedMessage()
.setDeniedCloseBtn()
.setDeniedSettingBtn()
.setRationalMessage()
.setRationalBtn()*/
.build(),
new AcpListener() {
@Override
public void onGranted() {
//授权成功
}
@Override
public void onDenied(List<String> permissions) {
Toast.makeText(MainActivity.this, "权限被拒绝", Toast.LENGTH_SHORT).show();
//授权失败,参数为失败的权限列表
}
});